I usually use constexpr const char*
for defining string constants. Off late, I have been noticing that std::string_view
is very popular for this purpose. Which of these is the best?
// 1. char pointer
constexpr const char* CP_CONST = "Char Pointer";
// 2. char array
constexpr char CA_CONST[] = "Char Array";
// 3. string_view
constexpr std::string_view SV_CONST{"String View"};
I understand in the first case, the string Char Pointer
is always going to reside in memory for the duration of the program.
In the second case, the string Char Array
will be destroyed after the array is created. And the array gets destroyed only when the scope ends.
In the third case also, the string String View
will reside in memory for the duration of the program.
Is there any reason to prefer one over the others for global char constants?
Thank you