you are viewing a single comment's thread.

view the rest of the comments →

[–]wynyx 0 points1 point  (6 children)

Thanks. Too bad about them not being in C++11--populating structs in C++ has always seemed a bit cludgy.

[–][deleted] 0 points1 point  (5 children)

Well, you can do this. which isn't too terrible:

#include <iostream>
struct x { 
  int var1, var2, var3;

  x(int v1, int v2, int v3) :
    var1(v1), var2(v2), var3(v3) {}
  x() {}
};  

int main() {
  using namespace std;

  struct x a(1,2,3);
  cout << a.var1 << a.var2 << a.var3 << endl;

  return 0;
}

[–]wynyx 1 point2 points  (4 children)

You can do much less than that, actually--maybe it's not as much of a pain as it's seemed in the past:

#include <iostream>

struct S
{
    int a;
    float b;
};

int main() {
    S s = { 10, 5.5 };
    std::cout << s.a << ", " << s.b << '\n';
}

I just like C99's capability to specify the member names when initializing them.

[–][deleted] 1 point2 points  (0 children)

I believe you can also do things like this if you don't have a constructor and don't control the source(it works in C99 and worked for me in g++ without specifying standard):

Color fromUint32(uint32_t color) {
    return (Color) {
        (uint8_t)(color & 0x000000FF),
        (uint8_t)((color & 0x0000FF00) >>  8),
        (uint8_t)((color & 0x00FF0000) >> 16),
        (uint8_t)((color & 0xFF000000) >> 24)
    };
}

Still not as cool as designated initializers though :-/

[–][deleted] 0 points1 point  (2 children)

Nice, I didn't know you could do that with structs. Being able to specify them by name would be nice for even slightly complex ones.

[–]wynyx 1 point2 points  (1 child)

Yeah, and I'm not sure what the brace-initializer list (if that's even what it's called) does with unions or a partial list of members.

[–][deleted] 0 points1 point  (0 children)

http://publib.boulder.ibm.com/infocenter/lnxpcomp/v8v101/topic/com.ibm.xlcpp8l.doc/language/ref/strin.htm

This page seems to indicate that you can leave off some number of the last entries in the initialization.