3

In a constexpr function, I am unable to define a non-literal variable within the branch of an if statement conditioned by C++20's std::is_constant_evaluated()? Clang and GCC both indicate it is not permitted, but in the example below, other constructs which cannot be evaluated at compile-time are permitted. Is there a specific restriction on the use of non-literals?

#include <type_traits> struct Foo { ~Foo() {} }; void non_constexpr() {} constexpr bool bar() { if (std::is_constant_evaluated()) { } else { non_constexpr(); double d; reinterpret_cast<int*>(&d); Foo f; // error: variable ‘f’ of non-literal type ‘Foo’ in ‘constexpr’ function } return true; } constexpr bool x = bar(); 
0

1 Answer 1

3

There is a specific restriction. In this case a structural restriction about the bodies of constexpr functions.

[dcl.constexpr]

3 The definition of a constexpr function shall satisfy the following requirements:

  • its function-body shall not enclose
    • a definition of a variable of non-literal type or of static or thread storage duration.

That's the long and short of it. If you are left wondering why it is so, given no forbidden code will be executed in constant evaluation, that's a good question too. Unfortunately I'm not aware of the answer to that at the moment. It might just be something no one has yet thought to change.

Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.