1

I'm working in gcc 4.8.1 and I have a question about its handling of POD/aggregate initialization. What I have is some plain C-like struct:

struct S { //.... };

which doesn't have any user-defined default-constructor.

But due to -Wall -Wextra being set for compilation, using syntax like:

S s = {};

or

S s; s = {};

generates warnings like:

warning: missing initializer for member 'one-of-S-members' [-Wmissing-field-initializers

Colleagues are using syntax like:

S s = S();

or

S s; s = S();

And, which is total suprise for me:

  1. There are no warnings
  2. What is even more confusing this properly initializes the structure just like aggregate syntax.

So the question is - is this some gcc-specific quirk?

The code is compiled with -std=c++11

Kara
  • 6,115
  • 16
  • 50
  • 57
altariste
  • 327
  • 2
  • 9
  • This is standard C++. Note that you can also say `S s{};`. – juanchopanza Feb 24 '14 at 13:35
  • I've always hated this warning. To my knowledge, as you say, it does work to value-initialize everything, yet GCC complains. – chris Feb 24 '14 at 13:40
  • Ok, but isn't this confusing? We have class with trivial implicitly-generated default constructor which does nothing and suddenly copying from it gives initialized structure? This would imply that the meaning of StructureType() is dependent on context? – altariste Feb 24 '14 at 13:41
  • @altariste, It's aggregate initialization with no initializers. Every member with a missing initializer is value-initialized in aggregate initialization. And yes, it would change to plain value-initialization if your class was no longer an aggregate. – chris Feb 24 '14 at 13:42
  • @chris I know how aggregate initialization works :) What bothers me is why StructureType s = StructureType() has the same effect :) – altariste Feb 24 '14 at 13:44
  • 4
    @altariste Why does that bother you? It is a value-initialized object on the RHS, which zero-initializes all members, and it is used in a *copy-initialization* of the LHS. – juanchopanza Feb 24 '14 at 13:46
  • @chris Ok thanks for explanation - one more question, is '=StructType()' syntax working only starting from C++11, or is this standard already in C++03? – altariste Feb 24 '14 at 13:51
  • And why gcc is little hypocrite and complains about missing initializers in one case but not the other? :) – altariste Feb 24 '14 at 13:52
  • @altariste, That's what you might call the normal way to value-initialize something in C++03. And you *can't* give that one members (only constructor arguments), so therefore, there's no missing members. I have no idea why it complains about the first (maybe to be helpful in case you *are* actually trying to give all members a value and forget one), but it's one of my least favourite warnings. – chris Feb 24 '14 at 13:53
  • @altariste As for the warning, see for example [What is the difference between these initializations (warning related)?](http://stackoverflow.com/q/21658485/341970). Together with [juanchopanza's comment](http://stackoverflow.com/questions/21989362/gcc-initializing-pod-types#comment33324205_21989362) it pretty much answers your question. – Ali Feb 24 '14 at 15:04

0 Answers0