0

Is it legal to aggregate initialize non-POD class types in ISO C++?

For example if we have a structure with a single method like this:

struct T
{
   operator double();

   int a;

   int b;
} ;

And we initialize an instance of it:

T tObj { 56, 92 };

using aggregate initialization. Is this legal?

Under Clang 3.7 it compiles fine although in VC++ 15 CTP 3 it doesn't.

Any insights on the question and a quote from the standard please?

AnArrayOfFunctions
  • 3,452
  • 2
  • 29
  • 66

1 Answers1

2

You can aggregate-initialise any aggregate, whether or not it's POD. C++11 defines an aggregate thusly:

[dcl.init.aggr] An aggregate is an array or a class with no user-provided constructors, no brace-or-equal-initializers for non-static data members, no private or protected non-static data members, no base classes, and no virtual functions

and your class meets that description.

C++14 relaxes the restrictions on aggregates, removing "no brace-or-equal-initializers for non-static data members"; that doesn't affect this question.

Note that your class is also POD; simply having a member function doesn't disqualify it. But being POD is largely unrelated to whether or not it's an aggregate.

Mike Seymour
  • 249,747
  • 28
  • 448
  • 644