C++20 added constexpr
std::string
s (among many other constructs that have until then not been constexpr
-able). According to the compiler compliance list at https://en.cppreference.com/w/cpp/compiler_support#cpp20, the three big compilers now support constexpr
std::string
operations. However, my simple attempts to exercise constexpr
strings have failed (https://compiler-explorer.com/z/c1can1P6Y). Consider the following:
#include <string>
constexpr std::string s = "abc"; // Global variable, supported by only GCC
int main()
{
constexpr std::string t = "def"; // Not supported by GCC 12, Clang 15, or MSVC 19.33
}
I would have expected both s
and t
to be constexpr
-constructible. But the fact that cppreference.com states all three compilers support constexpr
strings and yet Compiler explorer largely rejects my simple test above make me wonder if my expectations need to be adjusted.
The Question: With C++20, should I be able to constexpr
-construct both s
and t
above?