C++ 11只允許用戶定義模板字串文本運營商,用下面的模板簽名(taken from CppReference;返回類型並不需要是double
):爲什麼這個模板簽名不能像使用引號的字符串一樣工作?
template <char...> double operator "" _x();
然而,看來這僅適用在數字形式,而不是引用的形式:
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
GCC(5.1)或Clang(3.7)都不會給出有用的錯誤消息。 GCC說:
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)
鏘只是說:
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
那麼,爲什麼模板參數推導失敗?爲什麼字面操作符模板不匹配?
而且,通常情況下,是否可以修復使用情況,以便文字操作符可以與非數字字符串一起使用?
啊,我看 - 整數常量並且明確地說浮點文字被視爲一個函數調用'運算符「」 X <「C1」,「C2」,「C3 '...,'ck'>()'如果其他可能的文字操作符不可用。謝謝。 –