As constexpr std::string
and constexpr std::vector
have been accepted into C++20, how will these be used? The linked papers are very short on details. Do we need to specify special constexpr
allocators, making compile-time strings/vectors incompatible with their normal equivalents?
constexpr indicates that the value, or return value, is constant and, where possible, is computed at compile time. A constexpr integral value can be used wherever a const integer is required, such as in template arguments and array declarations.
However, std::string objects generally cannot be constexpr, because any dynamically allocated storage must be released in the same evaluation of constant expression.
Variable size container types, like std::vector or std::unordered_map , are generally useful for runtime programming, and therefore also potentially useful in constexpr computations. This has been made clear by some recent experiments such as the Constexpr ALL the things!
Formally, that's because vector constructor is not declared constexpr .
Those two papers depend heavily on P0784, which discusses how allocations at compile-time will work.
Incomplete answer:
std::allocator
will work.string
and vector
variables to be used at run-time. (Personally, I think there's a good chance that this restriction will be lifted in a future version of the standard - but that's just my opinion.)If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With