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:
- What magic is going on that allows an instance of
MyStruct1
to be initialized in that manner - 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.