Can constexpr be combined with volatile?

Yes, this is valid, there was defect report 1688: Volatile constexpr variables that was filed for this, saying:

There does not appear to be language in the current wording stating
that constexpr cannot be applied to a variable of volatile-qualified
type. Also, the wording in 5.19 [expr.const] paragraph 2 referring to
“a non-volatile object defined with constexpr” might lead one to infer
that the combination is permitted but that such a variable cannot
appear in a constant expression. What is the intent?

it was rejected as not a defect(NAD), the response and rationale was:

The combination is intentionally permitted and could be used in some
circumstances to force constant initialization.

As the DR points out such a variable is itself not usable in a constant expression:

constexpr volatile int i = 5;    
constexpr int y = i ;         // Not valid since i is volatile

Section [expr.const]/2 includes all the cases that makes a conditional-expression not a core constant expression including:

an lvalue-to-rvalue conversion (4.1) unless it is applied to

and all the exception require:

[…]that refers to a non-volatile […] object […]

Leave a Comment

Hata!: SQLSTATE[HY000] [1045] Access denied for user 'divattrend_liink'@'localhost' (using password: YES)