Why does `auto` not adopt the constexpr'ness of its initializing expression?
问题 Why doesn't defining a variable with auto keyword carry the constexpr 'ness of the expression used to initialize it? As an example, consider the following code: #include <string_view> constexpr std::string_view f() { return "hello"; } static constexpr std::string_view g() { constexpr auto x = f(); // (*) return x.substr(1, 3); } int foo() { return g().length(); } With GCC 10.2 and --std=c++20 -fsanitize=undefined -O3 , this compiles into: foo(): mov eax, 3 ret But if we remove the constexpr