2

How can one value-initialize aggregate types in C++14 with the list-intialization syntax?

Aggregate_t {}; 

This is seen as aggregate initialization, which produces errors or warnings for uninitialized members of Aggregate_t.

Is this possible at all?

EDIT: examples

struct Aggregate_t {
  int x;
};

int main (int, char**)
{
  Aggregate_t {};
  return 0;
}

Compiling with g++-4.9.2:

main.c++: In function ‘int main(int, char**)’:
main.c++:7:16: warning: missing initializer for member ‘Aggregate_t::x’ [-Wmissing-field-initializers]
   Aggregate_t {};
            ^
5
  • 5
    Please post an MCVE, together with the errors you get. Commented Feb 23, 2015 at 14:01
  • 2
    This should have the same effect as value-initialization (unless something changed since C++11 - I'm not entirely up-to-date yet). Do you actually get errors, or just over-zealous warnings? Also, please show us the definition of Aggregate_t so we can check it really is an aggregate. Commented Feb 23, 2015 at 14:02
  • It might be an overzealous warning, but the question is more about the effects of initialization. If uninitialized members of aggregate initialization are value-initialized (according to The Standard), then I guess this is the answer to my question. Commented Feb 23, 2015 at 14:10
  • @KarolyHorvath except that {} does initialize that member. Commented Feb 23, 2015 at 14:17
  • I see this as an over-zealous warning, and usually disable it by default. Commented Feb 23, 2015 at 14:17

1 Answer 1

3

[dcl.init.aggr]:

7 - If there are fewer initializer-clauses in the list than there are members in the aggregate, then each member not explicitly initialized shall be initialized [C++14: from its brace-or-equal-initializer or, if there is no brace-or-equal-initializer,] from an empty initializer list (8.5.4).

So g++ is being overzealous with its warning; I don't know of a way to avoid it while preserving it in cases where the warning is valid, except of course to use copy-initialization with expected copy elision:

Aggregate_t a = Aggregate_t();
Sign up to request clarification or add additional context in comments.

3 Comments

I also found it in cppreference.com: "If the number of initializer clauses is less than the number of members or initializer clauses is completely empty, the remaining members are initialized by their brace-or-equal initializers, if provided in the class definition, and otherwise by empty lists, which performs value-initialization." So, it generally should have the same effect as value-initializing.
The point would be not to have to resort to -() syntax to avoid gcc's warnings. Seems like it cannot be done, so, thanks!
As the name of the warning suggests, -Wmissing-field-initializers is for, well, missing initializers—even when the underlying thing is being initialized (as per manual). It is however designed to have exceptions, empty braces being one of them. In my experience GCC is not very good at this, so I would recommend -Wno-missing-field-initializers if you are confident it will not hide mistakes.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.