I'm getting a strange compiler error when trying to create constexpr
std::string
and std::vector
objects:
#include <vector>
#include <string>
int main()
{
constexpr std::string cs{ "hello" };
constexpr std::vector cv{ 1, 2, 3 };
return 0;
}
The compiler complains that "the expression must have a constant value":
Am I missing something? I am using the latest Microsoft Visual Studio 2019 version: 16.11.4, and the reference (https://en.cppreference.com/w/cpp/compiler_support) states that constexpr
strings and vectors are supported by this compiler version:
I have also tried the constexpr std::array
, which does work. Could the issue have anything to do with the dynamic memory allocation associated with vectors?