Register a SA Forums Account here!
JOINING THE SA FORUMS WILL REMOVE THIS BIG AD, THE ANNOYING UNDERLINED ADS, AND STUPID INTERSTITIAL ADS!!!

You can: log in, read the tech support FAQ, or request your lost password. This dumb message (and those ads) will appear on every screen until you register! Get rid of this crap by registering your own SA Forums Account and joining roughly 150,000 Goons, for the one-time price of $9.95! We charge money because it costs us money per month for bills, and since we don't believe in showing ads to our users, we try to make the money back through forum registrations.
 
  • Post
  • Reply
WHERE MY HAT IS AT
Jan 7, 2011

The input is coming from the keyboard, into a string object with getline(). I would like to be able to display the time on screen if possible, but it isn't strictly required. The timer_create() method seems to work, but I'm having a problem with that. When I use the timer_set() function, it doesn't seem to matter what I set the variables to in the struct that's passed in, it always returns instantly. Here's the code I have for it:
code:
struct itimerspec timer = {{10, 10000}, {0, 0}};

timer_settime(timerID, TIMER_ABSTIME, &timer, NULL);
I suspect the problem may have something to do with the TIMER_ABSTIME flag, but I couldn't seem to find any other possible flags that could be set. I'm sorry to ask again, but help here would be great. I sent an email to my prof but he generally takes forever to respond, and I can't seem to find any good examples to work from. I don't fully understand how this set of functions works so I'm having difficulty getting something that works properly. On the plus side, I have the signal handler set up properly and the function gets called, which seems like it should be most of the problem out of the way.

Adbot
ADBOT LOVES YOU

pseudorandom name
May 6, 2007

You don't have to pass any flags at all, use 0.

WHERE MY HAT IS AT
Jan 7, 2011
Weird. I had tried passing it NULL as I'm used to working in VS where NULL is defined a 0 already, I guess this isn't the case here though. That's done it, thank you!

pseudorandom name
May 6, 2007

NULL in gcc is either ((void*)0) in C or __null in C++, neither of which will convert to an integer without warning because ints and pointers aren't the same size these days.

shrughes
Oct 11, 2008

(call/cc call/cc)

pseudorandom name posted:

NULL in gcc is either ((void*)0) in C or __null in C++, neither of which will convert to an integer without warning because ints and pointers aren't the same size these days.

Not true, if you include the right header file NULL will be replaced by 0 in C++ and produce surprise behavior in certain situations.

Edit: Maybe I'm actually thinking of clang. But I think it's with gcc.

That Turkey Story
Mar 30, 2003

NULL is defined differently in different implementations, behaves differently in C than it does in C++, and requires certain headers to be included in order for it to be used. I know many people disagree, but it's just gross to use in C++, especially in C++11. Use 0 or nullptr if your compiler supports it. I cringe whenever I see NULL and I'm far from one of those "macros are evil" people.

pseudorandom name
May 6, 2007

shrughes posted:

Not true, if you include the right header file NULL will be replaced by 0 in C++ and produce surprise behavior in certain situations.

Edit: Maybe I'm actually thinking of clang. But I think it's with gcc.

I looked at both gcc's and clang's stddef.h before quoting what NULL is, it's ((void*)0) or __null. (Granted, they both have #define NULL 0 clauses for other compilers, but that isn't particularly relevant.)

I suspect you're referring to the plethora of headers from bad third-party libraries that unconditionally #undef and then redefine NULL to be ((void*)0), which does break when used from C++.

shrughes
Oct 11, 2008

(call/cc call/cc)

pseudorandom name posted:

I looked at both gcc's and clang's stddef.h before quoting what NULL is, it's ((void*)0) or __null. (Granted, they both have #define NULL 0 clauses for other compilers, but that isn't particularly relevant.)

I suspect you're referring to the plethora of headers from bad third-party libraries that unconditionally #undef and then redefine NULL to be ((void*)0), which does break when used from C++.

I wouldn't be so sure about that. The only third party library we'd realistically include in the file I remembered it in is Boost. Well maybe a protobuf header or v8 header got included, but that's all the non-system header coverage we have. protobuf is a more likely culprit than v8.

JasH
Jun 20, 2001

Hiya,

I am toying a bit with C++ on my Raspberry Pi, but I am not yet very familiar with either...

I am currently building an application which communicates wirelessly (not WiFi) with an Arduino.
My idea was to have a C++ application looping on my rPi which handles the communication with my Arduino and have a CGI application (also in C++) which I can use to send commands from a webbrowser to the Arduino.

The problem I have is that I don't know how I can communicate from my CGI application to my looping C++ application.
The simplest solution would be to write my commands from the CGI application to a text file and have the C++ application check it, but I'd rather avoid this setup...

Any suggestions?

Posting Principle
Dec 10, 2011

by Ralp

JasH posted:

Hiya,

I am toying a bit with C++ on my Raspberry Pi, but I am not yet very familiar with either...

I am currently building an application which communicates wirelessly (not WiFi) with an Arduino.
My idea was to have a C++ application looping on my rPi which handles the communication with my Arduino and have a CGI application (also in C++) which I can use to send commands from a webbrowser to the Arduino.

The problem I have is that I don't know how I can communicate from my CGI application to my looping C++ application.
The simplest solution would be to write my commands from the CGI application to a text file and have the C++ application check it, but I'd rather avoid this setup...

Any suggestions?

Take a look here. In particular, you may be interested in Pipes or Domain Sockets.

JasH
Jun 20, 2001

Jerry SanDisky posted:

Take a look here. In particular, you may be interested in Pipes or Domain Sockets.

That's exactly what I need; Thanks!

Papes
Apr 13, 2010

There's always something at the bottom of the bag.
Trying to implement Prim's algorithm to find minimum spanning trees, and not having any luck at all.

Here's what I currently have:

code:

struct Node {
    int index;
    vector<Edge> neighbors;
    bool visited;
    int distance; // not path weight

    Node() {
        visited = false;

    }

    void setDistance(int x) {
        (this->distance = x);
    }
};

struct Edge {
    pair<Node, Node> path;
    int e_index;
    int weight;

};

struct CompareNodes {

    bool operator () (const Node &l, const Node & r) const {
        return l.distance > r.distance;
    }
};

code:
void prim(vector<Node> vertex, int s) {
    vector<Node> copy(vertex);
    vector<Node> spanningTree;
    vector<Node> tree;
    Node v, u;
    Edge w;
    bool check;


    for (int i = 0; i < copy.size(); i++) {

        if (i == s) {
            copy[i].distance = 0;


            copy[i].visited = true;


        } else {
            copy[i].distance = 10000;


        }
        tree.push_back(copy[i]);
    }


    int minweight = 99999;
    int minIndex = 0; // minIndex will keep the index for the node with the smallest weight
    int tmp = 0;
    int totalCost = 0;


    while (tree.size() != 0) {
        make_heap(tree.begin(), tree.end(), CompareNodes());
        v = tree[0];
        pop_heap(tree.begin(), tree.end(), CompareNodes());
        tree.pop_back();

        for (int i = 0; i < v.neighbors.size(); i++) {
            check = false;

            if (v.neighbors[i].weight < v.neighbors[i + 1].weight &&
                   v.neighbors[i].weight < minweight &&
                   v.neighbors[i].path.second.visited == false) {
                   minweight = v.neighbors[i].weight;
                   u = v.neighbors[i].path.second;
                   w = v.neighbors[i];
                   check = true;
            }

            if (check == true && v.neighbors[i].weight > v.neighbors[i + 1].weight &&
                    v.neighbors[i + 1].weight < minweight &&
                    v.neighbors[i + 1].path.second.visited == false) {
                   minweight = v.neighbors[i + 1].weight;
                   u = v.neighbors[i + 1].path.second;
                   w = v.neighbors[i + 1];
            }
        }
        totalCost += minweight;
        minweight = 99999;
        u.distance = w.weight;
        u.visited = true;

        for (int i = 0; i < tree.size(); i++) {
            if (u.index == tree[i].index) {
                tree[i].setDistance(u.distance);

            }

        }

        spanningTree.push_back(v);



    }
    cout << totalCost << endl;
    for (int i = 0; i < spanningTree.size(); i++) {
        cout << spanningTree[i].index << " ";
    }
  
}

I feel like I'm rather close as the first 5-6 nodes in the tree are all correct, but then it starts to choose incorrect nodes.

I feel like the problem most likely lies in the implementation of these if-statements

code:

if (v.neighbors[i].weight < v.neighbors[i + 1].weight &&
                    v.neighbors[i].weight < minweight &&
                    v.neighbors[i].path.second.visited == false) {
                   minweight = v.neighbors[i].weight;
                   u = v.neighbors[i].path.second;

                w = v.neighbors[i];
                check = true;
            }

            if (check == true && v.neighbors[i].weight > v.neighbors[i + 1].weight &&
                    v.neighbors[i + 1].weight < minweight &&
                    v.neighbors[i + 1].path.second.visited == false) {
                   minweight = v.neighbors[i + 1].weight;
                   u = v.neighbors[i + 1].path.second;
                   w = v.neighbors[i + 1];
            }

I'm sure it's something pretty obvious, but I haven't really slept much in a few days so my eyeballs are useless.

nielsm
Jun 1, 2009



I think your problem is that your vectors contain copies of the nodes and edges, not references. So you actually build a large tree or something like that, when you build your graph.

E.g. this:
C++ code:
Node n1, n2;
Edge e1;
n1.index = 1;
n2.index = 2;
e1.path = std::make_pair(n1, n2);
n1.neighbors.push_back(e1);
You could maybe think this built a simple graph with two nodes and one edge from the first to the second. It does not. Instead you will have n1.neighbors[0].path.first be a different object from n1, meaning that this assertion will fail:
C++ code:
n1.visited = true;
assert(n1.neighbors[0].path.first.visited == true); // fails, given the previous setup
For a graph like this to work you need to use references of some kind.

One way of doing this would be to make a Graph class that has "master lists" of all the nodes and edges. Each node and edge then only reference other nodes/edges by their index into the vectors of them contained in the Graph. Additionally, each node and edge could possibly contain a pointer back to the Graph object, but I don't think that's necessary.

Papes
Apr 13, 2010

There's always something at the bottom of the bag.

nielsm posted:


For a graph like this to work you need to use references of some kind.

One way of doing this would be to make a Graph class that has "master lists" of all the nodes and edges. Each node and edge then only reference other nodes/edges by their index into the vectors of them contained in the Graph. Additionally, each node and edge could possibly contain a pointer back to the Graph object, but I don't think that's necessary.

Yeah, I think you might be right on that. However I'm not sure I really follow your suggestion. Are you saying that the containers should be in the Graph class and everything should be referenced by container index?

Something like

code:
Struct Graph{
vector<Nodes> nodes;
vector<Edges < edges:
};
?

nielsm
Jun 1, 2009



Papes posted:

Yeah, I think you might be right on that. However I'm not sure I really follow your suggestion. Are you saying that the containers should be in the Graph class and everything should be referenced by container index?

Yes.

C++ code:
struct Node {
  std::vector<size_t> outgoing_edges;
  bool visited;
};
struct Edge {
  size_t node_from;
  size_t node_to;
  int weight;
};
struct Graph {
  std::vector<Node> nodes;
  std::vector<Edge> edges;
};
I use the size_t type for the indexes into the Graph since that's generally what std::vector<>::size_type maps to.

Doing this, there is exactly one copy of each Node and each Edge object, and you always reference them through the Graph object. This also has the advantage that a graph has a clear "identity", your initial approach, if it could have worked, would make a graph representable by any collection of nodes and/or edges, or even just a single one if the entire graph was reachable from it in some way. But that's probably not so relevant in this case.

One thing you might consider is to use std::map<int,Node> and std::multimap<int,Edge> instead, depending on how you initially create your graph data structure. If you read it in from a file you probably give each node an ID or some such, and having a direct mapping from ID to each Node object, and a mapping from the outgoing node's ID to all edges starting at that node, would make set-up easier to manage, and possibly also algorithm implementation easier.

seiken
Feb 7, 2005

hah ha ha
I have a (mathematical) vector class which stores its elements in an array. However it'd be nice if I could also access the elements with their common names (v.x, v.y, etc) when the vector is a small enough size.

My first try at a solution:

C++ code:
template<typename T, std::size_t N>
stuct vec_accessors {
  vec_accessors(T* elements) {}
};

template<typename T>
struct vec_accessors<T, 3> {
  vec_accessors(T* elements)
    : x(*elements)
    , y(*(1 + elements))
    , z(*(2 + elements))
  {}

  const T& x; const T& y; const T& z;
};

// ... and so on ...

template<typename T, std::size_t N>
class vec : vec_accessors<T, N> {
public:

  T elements[N];

  vec()
    : vec_accessors<T, N>(elements)
    , elements{0}
  {
  }

  // ...

};
This basically works; the problem is that if I make the references x, y etc non-const (so that I can mutate the vector through them), then I can even mutate a const vec through them since the const-ness doesn't propagate through references.

It's not the end of the world, I could always add functions and use v.x(), or just use the long names v.elements[0] for mutating, just wondering if there's a better way?

seiken fucked around with this message at 19:09 on Apr 20, 2013

harvestsun
Apr 17, 2013
seiken, you could try creating an accessor class to wrap the "x", "y", and "z" references. You may be able to get your desired behavior using a fancy combination of cast operators.

Or of course you can just give in and resort to using:
code:
//1. 
vec.x() = val;

//2.
struct vector3 { 
//...
};

//3.
#define x elements[0] // trololololo
C++ really doesn't handle const-ness in as complete of a fashion as I would like. One of my few gripes with the language. It's hard to do tricky stuff like this (unless I'm forgetting something obvious).

That Turkey Story
Mar 30, 2003

seiken posted:

I have a (mathematical) vector class which stores its elements in an array. However it'd be nice if I could also access the elements with their common names (v.x, v.y, etc) when the vector is a small enough size.

My first try at a solution:

C++ code:
template<typename T, std::size_t N>
stuct vec_accessors {
  vec_accessors(T* elements) {}
};

template<typename T>
struct vec_accessors<T, 3> {
  vec_accessors(T* elements)
    : x(*elements)
    , y(*(1 + elements))
    , z(*(2 + elements))
  {}

  const T& x; const T& y; const T& z;
};

// ... and so on ...

template<typename T, std::size_t N>
class vec : vec_accessors<T, N> {
public:

  T elements[N];

  vec()
    : vec_accessors<T, N>(elements)
    , elements{0}
  {
  }

  // ...

};
This basically works; the problem is that if I make the references x, y etc non-const (so that I can mutate the vector through them), then I can even mutate a const vec through them since the const-ness doesn't propagate through references.

It's not the end of the world, I could always add functions and use v.x(), or just use the long names v.elements[0] for mutating, just wondering if there's a better way?

Don't do that thing you're doing with the references. That const issue is just one of a slew of problems. You're making the type larger and making accesses harder to optimize, both of which I would imagine you would like you avoid for something as low-level as a vector type. Just make named accessor functions. As for alternatives, a similar question came up a few pages back I think. What I personally do is I have empty types that I use as accessors, so I can do things like my_vector[x] = 5; or my_vector[right] = 5, where "x" and "right" are just instances of those empty types and the vector type has operator[] overloaded accordingly. Simple accessor functions is more concise and I would just recommend that unless you really want or need something else.

harvestsun posted:

C++ really doesn't handle const-ness in as complete of a fashion as I would like. One of my few gripes with the language. It's hard to do tricky stuff like this (unless I'm forgetting something obvious).

What problem do you have with const? If you're talking about propagation, it's correct that const does not propagate with a pointer or reference. It wouldn't really make sense the other way around -- why would a const pointer imply that what it's pointing to is const?

harvestsun
Apr 17, 2013

That Turkey Story posted:

What problem do you have with const? If you're talking about propagation, it's correct that const does not propagate with a pointer or reference. It wouldn't really make sense the other way around -- why would a const pointer imply that what it's pointing to is const?

You may have a point, but I think the argument could also be made that it doesn't make sense that, in this situation, he's able to modify a const structure.

You could say "well he just has a badly designed class, it shouldn't be exposing a reference to its own data". But it would be NICE (in my opinion) if there was a way to distinguish between association and aggregation. To be able to say "this is a pointer to something that affects the object's state".

I've also run into situations with the opposite problem: I have some member variable which is being used internally (for performance optimization), which doesn't affect the object's state (as seen by external classes). But const-ness propagates to that variable, so I can't modify it from a const member function. I would like to able to say "this is something which DOESN'T affect the object's state", and have const-ness NOT propagate to that object...

And obviously these problems could be solved by designing the class differently (or just resorting to the dreaded const_cast), so maybe I'm talking out of my rear end. But I for one think it would be cool to have a bit more control.

raminasi
Jan 25, 2005

a last drink with no ice

harvestsun posted:

I've also run into situations with the opposite problem: I have some member variable which is being used internally (for performance optimization), which doesn't affect the object's state (as seen by external classes). But const-ness propagates to that variable, so I can't modify it from a const member function. I would like to able to say "this is something which DOESN'T affect the object's state", and have const-ness NOT propagate to that object...

That's what mutable is for.

seiken
Feb 7, 2005

hah ha ha

That Turkey Story posted:

Don't do that thing you're doing with the references. That const issue is just one of a slew of problems. You're making the type larger and making accesses harder to optimize, both of which I would imagine you would like you avoid for something as low-level as a vector type. Just make named accessor functions. As for alternatives, a similar question came up a few pages back I think. What I personally do is I have empty types that I use as accessors, so I can do things like my_vector[x] = 5; or my_vector[right] = 5, where "x" and "right" are just instances of those empty types and the vector type has operator[] overloaded accordingly. Simple accessor functions is more concise and I would just recommend that unless you really want or need something else.

Yeah, I knew it was gonna be bad for optimisation, but luckily you've come along with this v[x] idea that I really like. Thank you.

Edit: in case anyone else has this problem, this is what I came up with
C++ code:
template<std::size_t N>
struct element_accessor {};

template<typename T, std::size_t N>
class vec {
public:

  T elements[N];

  template<std::size_t M, typename std::enable_if<(N > M), int>::type = 0>
  T& operator[](const element_accessor<M>& e)
  {
    return elements[M];
  }

  template<std::size_t M, typename std::enable_if<(N > M), int>::type = 0>
  const T& operator[](const element_accessor<M>& e) const
  {
    return elements[M];
  }

  // ...

};

namespace {
  const element_accessor<0> x;
  const element_accessor<1> y;
  const element_accessor<2> z;
  const element_accessor<3> w;
}

seiken fucked around with this message at 14:27 on Apr 21, 2013

That Turkey Story
Mar 30, 2003

harvestsun posted:

You may have a point, but I think the argument could also be made that it doesn't make sense that, in this situation, he's able to modify a const structure.
Not really. The problem is that what he wants to have happen with the reference doesn't really make sense. He's trying to use it as basically a textual alias for a contained object, which a reference is not. A reference aliases an object in a way that is similar to how a pointer does.

harvestsun posted:

You could say "well he just has a badly designed class, it shouldn't be exposing a reference to its own data". But it would be NICE (in my opinion) if there was a way to distinguish between association and aggregation. To be able to say "this is a pointer to something that affects the object's state".
There's no problem with exposing references to your own data in many cases, including his vector type. Specifically, that's what aggregate and "aggregate-like" types do (I.E. arrays, tuples, etc.). The "association" that you want can be achieved with functions, as is done with operator[] of std::array or std::get of std::tuple. The problem with the user-defined vector type here is how the reference types are used, it is not with their semantics. Reference datamembers are just not the correct tool for the job (they're rarely the best choice).

harvestsun posted:

I've also run into situations with the opposite problem: I have some member variable which is being used internally (for performance optimization), which doesn't affect the object's state (as seen by external classes). But const-ness propagates to that variable, so I can't modify it from a const member function. I would like to able to say "this is something which DOESN'T affect the object's state", and have const-ness NOT propagate to that object...
As was pointed out, that's what the keyword mutable is for. You use it when changing the member doesn't change the state of the object (I.E. it doesn't change the result of operator == and operator !=, if defined, when that particular member is modified). It's for things like caching results, counters that don't affect state, etc. Also, realize that this situation is actually different from situation with reference datamembers since indirection is not involved.

seiken posted:

Yeah, I knew it was gonna be bad for optimisation, but luckily you've come along with this v[x] idea that I really like. Thank you.

Edit: in case anyone else has this problem, this is what I came up with
Something along those lines should be fine. Depending on whether or not you declare an explicit default constructor will affect your ability to do those declarations of x, y, and z in that manner, at least in a standard-compliant compiler (I think VC++ lets you get away with it).

Edit: There are technically potential ODR issues. If you're a pedant, the "correct" way to do the declaration of those x, y, and z names is either with externed variables or with references. It's probably unnecessary, though boost often does little dances for declaring objects like this. Here is how Boost.Units does it, for example (this macro is used for things like "meters" and "seconds" when you make a quantity like 5 * meters / second).

That Turkey Story fucked around with this message at 17:05 on Apr 21, 2013

seiken
Feb 7, 2005

hah ha ha

That Turkey Story posted:

Something along those lines should be fine. Depending on whether or not you declare an explicit default constructor will affect your ability to do those declarations of x, y, and z in that manner, at least in a standard-compliant compiler (I think VC++ lets you get away with it).

Edit: There are technically potential ODR issues. If you're a pedant, the "correct" way to do the declaration of those x, y, and z names is either with externed variables or with references. It's probably unnecessary, though boost often does little dances for declaring objects like this. Here is how Boost.Units does it, for example (this macro is used for things like "meters" and "seconds" when you make a quantity like 5 * meters / second).

Thanks for the ODR info, I've fixed that. I'm not sure what difference an explicit default constructor would make here, though. For the record I'm using gcc 4.7 with -Wall, which didn't seem to complain about either of the two things you pointed out.

seiken fucked around with this message at 17:31 on Apr 21, 2013

That Turkey Story
Mar 30, 2003

seiken posted:

Thanks for the ODR info, I've fixed that. I'm not sure what difference an explicit default constructor would make here, though. For the record I'm using gcc 4.7 with -Wall, which didn't seem to complain about either of the two things you pointed out.

Apparently GCC has stopped emitting an error for the initialization. It used to, and was correct in doing so.

decl.init paragraph 6 posted:

pre:
To default-initialize an object of type T means:
  — if T is a (possibly cv-qualified) class type (Clause 9), the default constructor for T is called (and the
initialization is ill-formed if T has no accessible default constructor);
  — if T is an array type, each element is default-initialized;
  — otherwise, no initialization is performed.

If a program calls for the default initialization of an object of a const-qualified type T, T shall be a class type
with a user-provided default constructor.

The key point being that last sentence. I think it's a silly restriction, but whatever.

As for the ODR issue -- it wouldn't warn/error for a couple of reasons. First, since you're not actually using the object here in a way that would violate ODR, there's nothing wrong. Second, even if you were doing so, I don't think any compilers/linkers would notice, and unless you are doing something particularly sneaky, it's unlikely that you would ever encounter any problems in this case (the case being a stateless, dummy object that is only used for its type).

harvestsun
Apr 17, 2013

GrumpyDoctor posted:

That's what mutable is for.

Can't believe I didn't know about mutable, that's exactly what I was looking for. You learn something every day... I gotta lurk here more often.

I have related question. Say I have a class Foo with some property bar which is only generated as needed (again, for performance). So I have an accessor function like so:

code:
Bar Foo::getBar() const {
    if (!generatedBar) {
        bar = generateBar();
    }
    return bar;
}
Would it be "correct" to declare bar as mutable, so I can modify it from within this function? Or is there a "better" way to get what I'm after?

harvestsun fucked around with this message at 19:19 on Apr 21, 2013

Plorkyeran
Mar 22, 2007

To Escape The Shackles Of The Old Forums, We Must Reject The Tribal Negativity He Endorsed
That sort of thing is the intended use case of mutable, so yes. Do keep in mind that by using mutable that way you're deciding that the fact that it's generated lazily and cached is an implementation detail rather than a part of the interface that external code should rely on.

That Turkey Story
Mar 30, 2003

harvestsun posted:

Can't believe I didn't know about mutable, that's exactly what I was looking for. You learn something every day... I gotta lurk here more often.

I have related question. Say I have a class Foo with some property bar which is only generated as needed (again, for performance). So I have an accessor function like so:

code:
Bar Foo::getBar() const {
    if (!generatedBar) {
        bar = generateBar();
    }
    return bar;
}
Would it be "correct" to declare bar as mutable, so I can modify it from within this function? Or is there a "better" way to get what I'm after?

Yeah, and this is also potentially a good use-case for boost::optional if you use boost, unless one of the reasons that you want it to be lazy is because its static type is very large. I think I remember people talking about proposing a std::optional for the next standard, too, but don't quote me on that. Either way, I find it very useful.

Plorkyeran
Mar 22, 2007

To Escape The Shackles Of The Old Forums, We Must Reject The Tribal Negativity He Endorsed
std::optional was accepted for C++14

Rothon
Jan 4, 2012
I just ran into a weird issue trying to compile fish with Clang 3.2 on Linux. It seems to have generated an infinite loop in some std::wstring method: http://pastebin.com/aaRnBQNs. That's the result of using the AUR script. Compiling straight from the source generates an infinite loop in a completely different function: http://pastebin.com/iyP4VfsF, this time from within fish's source.

GCC 4.8.0 compiles it just fine. Is this a Clang bug?

scissorman
Feb 7, 2011
Ramrod XTreme
I'm thinking of getting back into C++ after not using it for several years, therefore I'm looking for a good, up-to-date book on how to write C++ code.
I'm especially looking for advice on C++11 as well as the recommended programming style.
Are there any books you'd recommend?

shrughes
Oct 11, 2008

(call/cc call/cc)

scissorman posted:

I'm thinking of getting back into C++ after not using it for several years, therefore I'm looking for a good, up-to-date book on how to write C++ code.
I'm especially looking for advice on C++11 as well as the recommended programming style.
Are there any books you'd recommend?

What have you done with C++ so far? Are you relatively noobic? Would you benefit from reading Koenig & Moo (for pre-C++11 learnings)? To go from there into C++11 mostly involves looking at the Wikipedia C++11 page and being non-retarded.

Begall
Jul 28, 2008
This question might be a bit specific but I thought I'd give it a shot. I use a TBS TV-Tuner card with my Ubuntu system, which I just upgraded from 3.6 Kernel to 3.9RC7. TBS drivers are a mix of open source/closed source so they have to be reinstalled with every kernel upgrade. Making them is now failing with the following errors on one of the open source parts:

code:
Drivers/tbs-linux-drivers_v130318/linux-tbs-drivers/v4l/pvrusb2-i2c-core.c:39:24: error: expected ')' before 'int'
Drivers/tbs-linux-drivers_v130318/linux-tbs-drivers/v4l/pvrusb2-i2c-core.c:40:27: error: expected ')' before string constant
Drivers/tbs-linux-drivers_v130318/linux-tbs-drivers/v4l/pvrusb2-i2c-core.c:43:29: error: expected ')' before 'int'
Drivers/tbs-linux-drivers_v130318/linux-tbs-drivers/v4l/pvrusb2-i2c-core.c:44:26: error: expected ')' before string constant
Drivers/tbs-linux-drivers_v130318/linux-tbs-drivers/v4l/pvrusb2-i2c-core.c:48:6: error: expected ')' before 'int'
Drivers/tbs-linux-drivers_v130318/linux-tbs-drivers/v4l/pvrusb2-i2c-core.c:50:4: error: expected ')' before string constant
Drivers/tbs-linux-drivers_v130318/linux-tbs-drivers/v4l/pvrusb2-i2c-core.c:536:19: error: 
'THIS_MODULE' undeclared here (not in a function)


This is the file in question. My first question is, since these seem like basic syntax errors, why does it still compile fine against 3.6 and below? And secondly, since compiling seems to have failed purely due to this one file, is there anything I can do to fix it?

scissorman
Feb 7, 2011
Ramrod XTreme

shrughes posted:

What have you done with C++ so far? Are you relatively noobic? Would you benefit from reading Koenig & Moo (for pre-C++11 learnings)? To go from there into C++11 mostly involves looking at the Wikipedia C++11 page and being non-retarded.
I used it extensively for hobby projects -- mainly 3D games programming -- but that was some time ago.
Since then I've done mostly C and Java, so I'd say I'm probably ok at the basics but anything like proper style, using the standard library and other various intricacies is not my strength.

shrughes
Oct 11, 2008

(call/cc call/cc)
My recommendation is to read Koenig & Moo's Accelerated C++, and see how that affects your worldview. Either it'll be useful or it'll be quick and painless. Then... there are other books on C++ that border on insanity. My personal C++ learning pattern went something like 1. Try to read inscrutable completely awful textbook written by a retard who should be driven out of academia, 2. go to college and get Koenig & Moo, get the basics of RAII and implementing data structures down, 3. go off to Haskell Oz Scheme la la land for a couple years, 4. come back to C++ with an appreciation for what looks "dangerous" in programming. There is some other stuff (a job with C#) in between.

I'd say start with Koenig & Moo. That's pre-C++11, which is fine. Then write some stuff using boost::shared_ptr and boost::scoped_ptr and other deranged boost crap. Learning C++11 is pretty straightforward, except for a couple of parts, if you understand the pain it tries to solve.

Edit: Of course, maybe I'm following the "You should learn programming the way I did" fallacy. However, Accelerated C++ is a universally recommended book.

scissorman
Feb 7, 2011
Ramrod XTreme
How is Accelerated C++ compared to the The C++ Programming Language book?
I've still got an old edition of Stroustrup's book lying around and the new version is coming out soon, so I might get that instead.

minidracula
Dec 22, 2007

boo woo boo

scissorman posted:

How is Accelerated C++ compared to the The C++ Programming Language book?
I've still got an old edition of Stroustrup's book lying around and the new version is coming out soon, so I might get that instead.
I will be one of those people who says that you shouldn't use TC++PL to learn from, regardless of edition while you can use TC++PL to learn from, you will likely be better served by a book that's written as a more focused tutorial. It is, however, a book you'll want to have around to answer various "why?" and other detailed questions about the language.

I second shruges recommendation of Koenig & Moo; yes it is pre-C++11, but that is OK. It is a fast read if you're already comfortable and confident, and a great refresher or tutorial otherwise. Also note, Barbara Moo was added to is one of the authors of the Fifth Edition of C++ Primer, which is all new for C++11, so you could just use your current copy of TC++PL along with Accelerated C++ to get back up to speed, and then shell out bucks for C++ Primer, Fifth Edition if you want to learn New School C++. Then buy the new Stroustrup whenever after it comes out.

Edits: revised wording in an attempt to moderate the "learn a programming language the way I did", or, more accurately for me, "like the same books as me" sentiment, which is not the goal. This is just, like, my opinion, man. Also, it turns out Moo was added originally on the Fourth Edition of C++ Primer, which was not C++11-related, so I tried to revise and clarify this bit as well.

minidracula fucked around with this message at 20:59 on Apr 22, 2013

robostac
Sep 23, 2009

Begall posted:


This is the file in question. My first question is, since these seem like basic syntax errors, why does it still compile fine against 3.6 and below? And secondly, since compiling seems to have failed purely due to this one file, is there anything I can do to fix it?

Try adding a "#include <linux/module.h>" to the top.

MrMoo
Sep 14, 2000


VLA's too, :iia:

Vanadium
Jan 8, 2005

Should I wait for the C++14 version of TC++PL? :allears:

Adbot
ADBOT LOVES YOU

Hughlander
May 11, 2005

Vanadium posted:

Should I wait for the C++14 version of TC++PL? :allears:

At that point you might as well just wait for the C++17 version!

  • 1
  • 2
  • 3
  • 4
  • 5
  • Post
  • Reply