Due to the constraints of the domain I'm working with, I need to define string literals both as char const* and wchar const*, for example:
constexpr auto C_VAR_NAME_1 = "MY_VAR_NAME_1";
constexpr auto C_VAR_NAME_2 = "MY_VAR_NAME_2";
...
constexpr auto W_VAR_NAME_1 = L"MY_VAR_NAME_1";
constexpr auto W_VAR_NAME_2 = L"MY_VAR_NAME_2";
...
I have lots of constants and I'd like to avoid to define twice the same actual name of the variable (which could lead to some typo, hence unmatching names), so I've used some macro like this:
#define WTEXT_IMPL(name) L##name
#define WTEXT(name) WTEXT_IMPL(name)
#define MACRO_VAR_NAME_1 "MY_VAR_NAME_1"
#define MACRO_VAR_NAME_2 "MY_VAR_NAME_2"
...
constexpr auto C_VAR_NAME_1 = MACRO_VAR_NAME_1;
constexpr auto C_VAR_NAME_2 = MACRO_VAR_NAME_2;
...
constexpr auto W_VAR_NAME_1 = WTEXT(MACRO_VAR_NAME_1)
constexpr auto W_VAR_NAME_2 = WTEXT(MACRO_VAR_NAME_2)
...
This works but, if possible, I'd like to get rid of the macro stuff; so my question is: is it possible to achieve the same result at compile-time using plain C++ standard without macros? Thank you in advance for your help.
If you can limit string literal to ASCII characters only there is simple solution without use of macros (in version for C++17):
template<std::size_t N>
struct DoubleStringLiteral {
constexpr DoubleStringLiteral(const char(&s)[N])
{
for (size_t i=0; i<N; ++i) {
if (s[i] < 0) throw std::invalid_argument("None ASCII character are not supported!");
str[i] = s[i];
wstr[i] = static_cast<wchar_t>(s[i]);
}
}
constexpr std::string_view view() const
{
return { str };
}
constexpr std::string_view wview() const
{
return { wstr };
}
char str[N];
wchar_t wstr[N];
};
constexpr DoubleStringLiteral a{ "Demo!" };
constexpr DoubleStringLiteral b{ R"(Raw!
str
sample)" };
It should be doable for C++11, but lots of boiler plate code is needed.
Here is C++14 version.