C++ Logo

std-discussion

Advanced search

Re: Why is this code accepted by all compilers?

From: Jens Maurer <Jens.Maurer_at_[hidden]>
Date: Fri, 12 Nov 2021 09:25:39 +0100
On 12/11/2021 04.04, Hani Deek via Std-Discussion wrote:
> The following code is accepted by GCC, Clang and MSVC. I thought it was ill-formed because the variable V0 is not usable in constant expressions. Is this just a bug in all of those compilers? Or is there an explanation in the C++ rules?
>
> struct S{ constexpr operator int() const{ return 0; } };
>
> void foo(S V0) { constexpr int V1 = V0; }
>
> int main()
> {
>    S V0{}; 
>    foo(V0);
> }

There is no lvalue-to-rvalue conversion on any part of V0.

Do you feel any other provision in [expr.const] p5 should
apply, making this not a constant expression?

Jens

Received on 2021-11-12 02:25:46