C++11 permits user-defined template string-literal operators, with the following template signature (taken from CppReference; the return type does not need to be double
):
template <char...> double operator "" _x();
However, it appears that this only works in the numeric form, not the quoted form:
template <char... chs>
std::string operator ""_r()
{
// ... construct some sort of string and return it
}
// ...
auto my_str = "abc"_r; // ERROR (compiler messages copied below)
auto my_num = 123_r; // NO ERROR
Neither GCC (5.1) nor Clang (3.7) give a useful error message. GCC says:
error: no matching function for call to ‘operator""_r()’
// printed source elided
note: candidate: template<char ...chs> std::__cxx11::string operator""_r()
// printed source elided
note: template argument deduction/substitution failed:
// ERROR OUTPUT ENDS HERE
// (nothing is printed here, despite the ':' in the previous line)
Clang simply says:
error: no matching literal operator for call to 'operator "" _r' with arguments of types 'const char *' and 'unsigned long', and no matching literal operator template
So why does template argument deduction fail? Why does the literal operator template not match?
And, in general, is it possible to fix the usage so that template <char...>
literal-operators can be used with non-numeric strings?