constexpr std::string尝试创建and对象时出现奇怪的编译器错误std::vector:
#include <vector>
#include <string>
int main()
{
constexpr std::string cs{ "hello" };
constexpr std::vector cv{ 1, 2, 3 };
return 0;
}
Run Code Online (Sandbox Code Playgroud)
编译器抱怨“表达式必须具有常量值”:
我错过了什么吗?我使用的是最新的 Microsoft Visual Studio 2019 版本:16.11.4,并且参考(https://en.cppreference.com/w/cpp/compiler_support)指出constexpr此编译器版本支持字符串和向量:
我也尝试过constexpr std::array,它确实有效。该问题是否与向量相关的动态内存分配有关?