考虑:
struct str {};
str operator"" _X(long double d) {
return str();
}
这在 g++ 4.7.2 Wall std=c++11 下编译得很好
但现在如果我给双倍:
str operator"" _X(double d) {
return str();
}
我收到以下错误消息: main.cpp|3|错误:'str operator""_X(double)' 的参数列表无效
问题是什么?这与“无法重新定义内置文字后缀的含义”(Stroustrup FAQ)有关吗? 您能想出解决方法吗?
最佳答案
What is the problem?
问题是标准禁止它。根据 C++11 标准关于用户定义文字的第 13.5.8./3 段:
The declaration of a literal operator shall have a parameter-declaration-clause equivalent to one of the following:
const char* unsigned long long int long double char wchar_t char16_t char32_t const char*, std::size_t const wchar_t*, std::size_t const char16_t*, std::size_t const char32_t*, std::size_t
关于解决方法,我不确定它是否需要,因为以下工作正常(double
隐式转换为 long double
,因此您可以传入double
类型的文字):
struct str {};
str operator"" _X(long double d) {
return str();
}
int main()
{
str s = 4.0_X;
}
关于C++11 operator""with double parameter,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23429769/