C ++ 11运算符“带双参数 [英] C++11 operator"" with double parameter
问题描述
请考虑:
struct str {};
str operator"" _X(long double d) {
return str();
}
使用g ++进行编译4.7.2 Wall std = c ++ 11
This compiles fine with g++ 4.7.2 Wall std=c++11
但现在如果我给一个double:
but now if I give a double :
str operator"" _X(double d) {
return str();
}
我收到以下错误信息:
main.cpp | 3 |错误:str运算符_X(双)具有无效的参数列表
I get the following error message: main.cpp|3|error: 'str operator"" _X(double)' has invalid argument list
有什么问题?这有什么关系不可能重新定义内置文字后缀的意义(Stroustrup FAQ)?
What is the problem ? Has this something to do with "It is not possible to redefine the meaning of a built-in literal suffix" (Stroustrup FAQ) ? Can you think of a workaround ?
推荐答案
问题是什么? p>
What is the problem?
问题是,标准禁止它。根据用户定义文字的C ++ 11标准的第13.5.8./3节:
The problem is that the Standard forbids it. Per paragraph 13.5.8./3 of the C++11 Standard on user-defined literals:
文字操作符的声明有一个等价于
之一的参数声明子句:
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
类型的文字:
Concerning a workaround, I am not sure it is needed, since the following works fine (a double
gets implicitly converted to a long double
, so you can pass in literals of type double
):
struct str {};
str operator"" _X(long double d) {
return str();
}
int main()
{
str s = 4.0_X;
}
这篇关于C ++ 11运算符“带双参数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!