[cfe-dev] consteval constructor potentially evaluated at runtime?

Stephan Bergmann via cfe-dev cfe-dev at lists.llvm.org
Thu Oct 21 05:49:10 PDT 2021


On 9/11/20 14:25, Stephan Bergmann wrote:
> I'm a bit puzzled by the following which I observe at least with recent 
> Clang 12 trunk:
> 
>> $ cat test.cc
>> struct S {
>>     consteval S() {}
>>     int a;
>> };
>> constexpr S s1;
>> S s2;
> 
>> $ clang++ -std=c++20 -fsyntax-only test.cc
>> test.cc:5:13: error: constexpr variable 's1' must be initialized by a 
>> constant expression
>> constexpr S s1;
>>             ^~
>> test.cc:5:13: note: subobject of type 'int' is not initialized
>> test.cc:3:9: note: subobject declared here
>>     int a;
>>         ^
>> 1 error generated.
> 
> That is, while construction of the constexpr s1 is correctly rejected, 
> the plain s2 is accepted (and execution of the consteval constructor 
> appears to happen at runtime).
> 
> My understanding of latest draft [expr.const]/13
> 
>> An expression or conversion is in an immediate function context if it 
>> is potentially evaluated and its innermost
>> non-block scope is a function parameter scope of an immediate 
>> function. An expression or conversion is an
>> immediate invocation if it is a potentially-evaluated explicit or 
>> implicit invocation of an immediate function
>> and is not in an immediate function context. An immediate invocation 
>> shall be a constant expression.
> 
> would be that the construction of s2 is an immediate invocation, so 
> would need to be a constant expression.  Is that aspect of consteval 
> just not implemented yet in Clang, or is my understanding wrong?

I filed <https://bugs.llvm.org/show_bug.cgi?id=52251> "Invalid consteval 
constructor invocation does not cause an error" now.



More information about the cfe-dev mailing list