The C++ standard specifically bans calling new
in a constant expression (N4296 section 5.20 [expr.const]):
A conditional-expression e is a core constant expression unless the evaluation of e, following the rules of the abstract machine (1.9), would evaluate one of the following expressions:
...
— a new-expression (5.3.4);
This ban (as far as I can see) extends to all forms of new
, including placement new. However, since placement new doesn't actually allocate any memory and merely runs constructors at the given location, and since it's legal to take the address of a variable in a constexpr
context (indeed, std::addressof
will be constexpr in C++17), it seems to me that this prohibition could (in principle at least) be eased to allow placement new in constexpr functions.
So my question is, am I missing something? Is there a good reason why placement new is forbidden in constexpr
functions?
(For context: the current rules pretty much require that constexpr-enabled sum types like std::variant
are implemented as a recursive union. It would be nicer to be able to use something like std::aligned_storage
and placement new, but currently that's not possible.)
std::aligned_storage
and placement new" I don't think so, and given that reinterpret_cast is banned in constant expression, I don't think you can implementstd::variant
in this way even with constexpr placement new. – Newspaper