I'm piggy-backing as much as replying...
Sam <
s...@email-scan.com> writes:
> Juha Nieminen writes:
>
>> Why doesn't the last line compile?
>>
>> //------------------------------------------------------------
>> struct Test1 {};
>> struct Test2 { constexpr Test2() {} };
>>
>> struct Container
>> {
>> struct Test3 {};
>> struct Test4 { constexpr Test4() {} };
>>
>> static constexpr Test1 object1; // Compiles
>> static constexpr Test2 object2; // Compiles
>> static constexpr Test3 object3; // Compiles
>> static constexpr Test4 object4; // Does not compile
>> };
A data point... gcc (Ubuntu 7.3.0-16ubuntu3) 7.3.0 complains about all
four lines with "constexpr static data member ‘object1’ must have an
initializer". Adding = {} to each makes it complain only about the last
one with ‘constexpr Container::Test4::Test4()’ called in a constant
expression before its definition is complete".
> The definition of the "Container" class is not complete until its closing brace.
>
> A constexpr object must refer to a complete class, amongst all the
> other requirements. "Test4" refers to a member of an incomplete class,
> at the time it's used.
That's the bit that confuses me. Container is not complete, but Test4
looks complete. Adding
struct Test5 { int i; };
...
static constexpr Test5 object5 = {};
provokes no complaint (for gcc or clang) and Test5 looks just as
complete or incomplete as Test4. The issue must be specific to classes
with constructors and I confess to not have read what C++ says about a
class being complete.
> You just can't have a `constexpr` class member instance of the class
> itself, or of any inner classes.
gcc and clang both permit such a thing in the case of Test5, so is that
a compiler bug?
--
Ben.