This thing might or might not do what you want:
constexpr const char* check(const char* c) {
return c;
}
Usage:
int main() {
constexpr auto result = check("test");
constexpr auto result2 = check(std::string("test").c_str()); // error
}
The produced error message is:
main.cpp:11:61: error: call to non-constexpr function 'const _CharT* std::__cxx11::basic_string<_CharT, _Traits, _Alloc>::c_str() const [with _CharT = char; _Traits = std::char_traits<char>; _Alloc = std::allocator<char>]'
constexpr auto result2 = check(std::string("test").c_str());
~~~~~~~~~~~~~~~~~~~~~~~~~^~
The way it works isn't checking the literal directly; rather than that, it checks whether the const char*
value can be obtained at compile time, which, again, could be more meaningful, but not exactly what you want.