struct initialization to zeros like so {};

I've been seeing this sort of thing only fairly recently it seems ...

 
WNDCLASSEX wc={};


How long has this been legal? Is it some recent extension to the C++ standards? This seems to compile fine as a C++ program using a fairly recent C++ compiler ...
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
#include <cstdio>
#define BYTES 16

struct Etwas
{
 int    a;
 int    b;
 double c;
};

int main()
{
 Etwas obj={};
 char szBytes[BYTES]={};

 printf("obj.a=%d\n",obj.a);
 printf("obj.b=%d\n",obj.b);
 printf("obj.c=%f\n\n",obj.c);
 for(int i=0; i<BYTES; i++)
     printf("%d\t%c\n",i,szBytes[i]);
 getchar();

 return 0;
}


However, using VC6 (circa 1998 or so) and compiling as a C program, with the struct keyword in front of the Etwas struct, it won't compile. VC6 errors out on the {} brackets lines 12 and 13.

What I'm wondering is if I just failed to learn this or if its something recent? I've just never seen that idiom until recently. Can anyone tell me anything about it?
http://stackoverflow.com/questions/1352370/c-static-array-initialization-how-verbose-do-i-need-to-be/1352379#1352379

(circa 1998 or so)

The first C++ standard was ratified in 1998, so it very well could be that VC6 was not following the standard yet. In C (which would be support pre-standard) you had to have at least one statement inside the brackets.
geez! Some obscure stuff. That worked though. I just added the '0', and it compiled and ran OK ...

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
#include <cstdio>
#define BYTES 16

struct Etwas
{
 int    a;
 int    b;
 double c;
};

int main()
{
 struct Etwas obj={0};
 char szBytes[BYTES]={0};
 int i=0;

 printf("obj.a=%d\n",obj.a);
 printf("obj.b=%d\n",obj.b);
 printf("obj.c=%f\n\n",obj.c);
 for(i=0; i<BYTES; i++)
     printf("%d\t%c\n",i,szBytes[i]);
 getchar();

 return 0;
}
Here's the output from that, by thev way. The 1 - 15 numbers with nothing after them are from the loop, and all the bytes are null.

obj.a=0
obj.b=0
obj.c=0.000000

0
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
Topic archived. No new replies allowed.