The simplest solution is:
std::string DataDirHelper(const std::string& file) {
#ifndef CRYPTOPP_DATA_DIR
return file;
#else
std::string dataDir("" CRYPTOPP_DATA_DIR);
...
#endif
}
When CRYPTOPP_DATA_DIR
is a string literal, the compiler will merge it with the adjacent empty string. When it is not a string literal, it may still compile if the macro is sufficiently awful (with some leading commas and what not).
Alternatively, we could use the requirement that static_assert
requires a string literal as an argument. Thus, we could write this code:
std::string DataDirHelper(const std::string& file) {
static_assert(true, CRYPTOPP_DATA_DIR);
std::string dataDir("" CRYPTOPP_DATA_DIR);
...
}
If CRYPTOPP_DATA_DIR
was not a string literal, you would see an error message like this:
foo.cc:12:9: error: expected string literal
static_assert(true, CRYPTOPP_DATA_DIR);
^~~~~~~~~~~~~~~~~