2

I have this code for MS windows:

#include <windows.h>
#include <Shellapi.h>
#include <tchar.h>
#include <string>

void foo()
{
    SHELLEXECUTEINFO shExInfo = {0};
}

According to this answer What Does {0} Mean in C++?

What's happening here is called aggregate initialization. Here is the (abbreviated) definition of an aggregate from section 8.5.1 of the ISO spec:

An aggregate is an array or a class with no user-declared constructors, no private or protected non-static data members, no base classes, and no virtual functions.

This code compiles ok however on MinGW 4.9 it throws:

warning: missing initializer for member '_SHELLEXECUTEINFOW::lpParameters' [-Wmissing-field-initializers]

for every single part of SHELLEXECUTEINFO struct

Why is that? Did something change in specs? Note: this code is c++11

Community
  • 1
  • 1
Petr
  • 13,747
  • 20
  • 89
  • 144

2 Answers2

5

Why is that?

Because you've asked the compiler to warn about cases like this, either by specifying this particular error with -Wmissing-field-initializers, or a larger set of warnings with -Wextra or similar.

Usually, warnings are for code that's valid, but could indicate an error. In this case, it could indicate that you forgot to initialise something that needs initialisation, or just that you want the default behaviour.

If you don't like this warning, and don't feel like changing the code to explicitly initialise all the fields, then you can disable it with -Wno-missing-field-initializers.

Did something change in specs?

No, it's still valid to only provide initialisers for some fields of an aggregate. That's why it's only a warning, not an error.

Note: this code is c++11

Then I'd change it to SHELLEXECUTEINFO shExInfo {}; to specify value-initialisation. This will zero-initialise all the fields, without triggering any warnings. In older dialects, = {} should have the same effect.

Mike Seymour
  • 249,747
  • 28
  • 448
  • 644
  • I was confused because MSVC compiler triggers no warnings at all for that code, which kind of make sense, I believe this code is taken from msdn examples. – Petr Dec 17 '14 at 09:14
  • BTW using SHELLEXECUTEINFO shExInfo {}; produces the same warning :/ it needs to be explicitly disabled, which I find pretty complex, when you are doing cross platform code that is to be compiled on dozen of compilers. (this block of code is windows specific, but compiler flags are handled through cmake) – Petr Dec 17 '14 at 14:17
2

Nothing changes in specs, it is just a warning that, if requested, will remind you to specify explicit initializers for all fields of the aggregate.

Meanfile, the initializer you wrote is perfectly legal form the language point of view.

Per specification of -Wmissing-field-initializers, you can shorten your initializer to = {}. The end effect will be the same, but without warnings. I.e. this is the way to tell the compiler that you do want to rely on implicit value-initialization of fields even in presence of -Wmissing-field-initializers.

But a better idea would be to check why you have -Wmissing-field-initializers enabled. This is one of those "extreme" warning options that complains about many very useful programming idioms. -Wmissing-field-initializers is not even included into -Wall. It is a part of -Wextra.

AnT stands with Russia
  • 312,472
  • 42
  • 525
  • 765