consteval constructor potentially evaluated at runtime?

classic Classic list List threaded Threaded
1 message Options
Reply | Threaded
Open this post in threaded view

consteval constructor potentially evaluated at runtime?

Valeriy Savchenko via cfe-dev
I'm a bit puzzled by the following which I observe at least with recent
Clang 12 trunk:

> $ cat
> struct S {
>     consteval S() {}
>     int a;
> };
> constexpr S s1;
> S s2;

> $ clang++ -std=c++20 -fsyntax-only
> error: constexpr variable 's1' must be initialized by a constant expression
> constexpr S s1;
>             ^~
> note: subobject of type 'int' is not initialized
> 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?

cfe-dev mailing list
[hidden email]