0

edit Tweaked example a little based on comments

A little code then the question (just to clarify, this is a C++ question):

#include <cstdio>

struct MYSTRUCT1 {
  int asdf[4];
} MyStruct1;

struct MYSTRUCT2 {
  int asdf[4];
  MYSTRUCT2() : asdf() {}
} MyStruct2;

template <class T>
void test() {
  T blah = {{1,-1,1,-1}};

  for( int ii = 0; ii < 4; ii++ ) {
    printf( "%d ", blah.asdf[ii] );
  }
  printf( "\n" );
}

int main() {
  // Works fine; MyStruct1 doesn't define a constructor
  test<MyStruct1>();
  // Doesn't work; g++ complains I need to use `-std=c++0x`
  // and/or define a constructor that allows the initialization
  // taking place inside `test()`
  test<MyStruct2>();
}

There's a few questions in here:

  1. What magic is going on that allows an instance of MyStruct1 to be initialized in that manner
  2. Is there a workaround for this in c++98?

For reference, I'm attempting to use constructors as a means to force stack allocated structs to be zero initialized, but I don't want to inhibit this style of initialization.

Brian Vandenberg
  • 4,011
  • 2
  • 37
  • 53
  • possible duplicate of [When can outer braces be omitted in an initializer list?](http://stackoverflow.com/questions/11734861/when-can-outer-braces-be-omitted-in-an-initializer-list) – Nawaz Apr 11 '13 at 16:39
  • 1
    `main` needs a return type, Skippy. If this is C++, why the antiquated `typedef struct` idiom? `printf`? – Lightness Races in Orbit Apr 11 '13 at 16:40
  • Do you really think it contributes /any/ value to the question to comment on how main is defined? See http://stackoverflow.com/questions/1621574/mains-signature-in-c for a clarification. `int` is assumed if not explicitly stated, and this is a terse write-up to exemplify something completely unrelated, not code actually in use. – Brian Vandenberg Apr 11 '13 at 16:45
  • 1
    @BrianVandenberg: Er, no, `return 0` is assumed if not given, but the return type _must_ be provided for a compliant program. When you actually _read_ the posts you linked me to, you'll see that what I am telling you is the truth. / What does it contribute? It contributes value to the quality of your code. For free, on my own time. _You're welcome._ – Lightness Races in Orbit Apr 11 '13 at 16:48
  • w/regard to the use of the `typedef`, old habits and all that. I'm using `struct` for a few reasons, among them: I'm modifying old code and distilling the original author's code down to a simplified example for testing. – Brian Vandenberg Apr 11 '13 at 16:51
  • My apologies, I was wrong. Although many compilers will infer `int` as the return type, the standard doesn't strictly allow it (eg: `echo 'blah(){}' | clang++ -xc++ -c -o blah -` => `error: C++ requires a type specifier for all declarations`) – Brian Vandenberg Apr 11 '13 at 17:27
  • Also, one last remark. You were absolutely correct. You did provide value by remarking; I learned something I didn't know. – Brian Vandenberg Apr 11 '13 at 17:40

1 Answers1

1

What magic is going on that allows an instance of MyStruct1 to be initialized in that manner

Well, there's no "magic" per se. MyStruct1 is an aggregate type but, thanks to the ctor, MyStruct2 is not. You're attempting to perform aggregate initalisation, which may only be successful on an object of an aggregate type.

Is there a workaround for this in c++98?

Make your constructor do its job and take the arguments you need for initialisation.

Trying to use constructors to first zero-initialise everything seems like you're sort of half-thinking in C and half-thinking in C++ (correlated by your use of the antiquated typedef struct idiom that has not been required in C++ for decades).

Lightness Races in Orbit
  • 378,754
  • 76
  • 643
  • 1,055
  • w/regard to your remark about first zero-initialising everything, I'm working through a lot of very antiquated code, a lot of which has very severe problems (use of uninitialized pointers, use of local structs that haven't been initialized, etc). There's too much code to just fix or replace it all, but in the short term if I can do something that forces the structs to be zero initialized so they're not using whatever's on the stack as their contents, that can at least help mitigate the more severe problems in these people's code. – Brian Vandenberg Apr 11 '13 at 16:58
  • 1
    @Brian: `[C++03: 8.5.1/1]: An aggregate is an array or a class (clause 9) with no user-declared constructors (12.1), no private or protected non-static data members (clause 11), no base classes (clause 10), and no virtual functions (10.3).` The wording in C++98 will be the same. – Lightness Races in Orbit Apr 11 '13 at 17:01
  • Thank you; I didn't know adding a constructor would affect whether it's considered an aggregate type. – Brian Vandenberg Apr 11 '13 at 17:33