Open ThePhD opened 5 years ago
Note that is_static_storage_duration
makes subtly different guarantees: notably, there may not be interning, it may not be a string literal, etc.
Submitted for LEWG, Köln, Germany 2019 for inclusion in C++23.
The utility of this is going to need more motivation; until then, it's going to be targeted at C++26 to take the pressure off.
Moved to C++29. It also might be impossible to implement at all, so it might be time to close this one entirely.
This seems (partially?) implementable if we can restore constant evaluation from failure.
template<class CharT>
consteval bool is_string_literal(const CharT* p) noexcept {
try consteval { // constant evaluation failure inside the try-consteval block is caught and ignored
while (*p != CharT{})
++p;
return true;
}
return false;
}
Perhaps the __builtin_constant_p
extension is already capatible for implementing the utility (Godbolt link)?
template<class CharT>
constexpr bool is_string_literal_helper(const CharT* p) noexcept {
while (*p != CharT{})
++p;
return true;
}
template<class CharT>
consteval bool is_string_literal(const CharT* p) noexcept {
return __builtin_constant_p(::is_string_literal_helper(p));
}
Determining whether a string is a string literal gives us certain guarantees about memory layout and null termination. The goal is to write a
consteval
function that covers this landscape area.