This is constexpr char * const my_str = "hello";
No, because a string literal is not converted to a pointer to a char . (It used to be before C ++ 11, but even then the conversion was deprecated).
or const char * constexpr my_str = "hello";
No. constexpr cannot get there.
It will be well formed:
constexpr const char * my_str = "hello";
but this does not satisfy:
So that I can get its length at compile time with sizeof, etc.
or constexpr char my_str [] = "hello";
This is well formed and you can get the length at compile time with sizeof . Note that this size is the size of the array, not the length of the string, i.e. the size includes a null terminator.
source share