I'm currently trying to use constexpr
to define some input identification stuff:
struct RangeParams {
string legacyId;
string fullname;
string shortname;
float min = -1;
float baseline = 0;
float max = 1;
float defaultValue = 0;
};
...
inline constexpr RangeParams curve1 = { "some_id", "my_name", ...};
Unfortunately, I get an error for the constexpr line saying
Constexpr variable cannot have non-literal type 'const RangeParams'
So, I dug into it to figure out what part of this is non-literal, and string
was the culprit.
std::cout << std::is_literal_type<float>::value; // output: 1
std::cout << std::is_literal_type<string>::value; // output: 0
Finding this has highlighted an important fact for me which is that my understanding of literal-types is rather flawed. For the most part, I'd simply thought of them as being the basics (numbers, strings, booleans, structs made of those things).
So why would a simple string not be a literal type? What's the gotcha here?
Also, how can I get around this? I'm trying to make a global out of my RangeParams and the most modern answer to this question (Defining global constant in C++) appears not be working.