c++0x 中用户定义文字的重载规则
我对重载规则有点困惑,
假设有以下文字运算符,
unsigned long long operator "" _xx(unsigned long long cooked_literal_int); //1
unsigned long long operator "" _xx(const char * raw_literal_string); // 2
unsigned long long operator "" _xx(long double cooked_literal_double); // 3
如果 1, 2, & 3 被定义,重载是明显的,
13_xx //call 1
13.5_xx //call 3
如果 1 & 3 2 被定义,
13_xx //call 1
13.5_xx //call 2
如果 2 &定义了 3 个
13_xx // call 2 or 3??
13.5_xx // call 3
混淆来自最新的 c++0x 标准 n3225 2.14.8/3,
如果 L 是用户定义的整数文字,则令 n 为不带 ud 后缀的文字。如果 S 包含参数类型为 unsigned long long 的文字运算符,则文字 L 被视为以下形式的调用
运算符“”X(n ULL)
否则,S 应包含原始文字运算符或文字运算符模板 (13.5.8),但不能同时包含两者。如果 S 包含原始文字运算符,则文字 L 被视为以下形式的调用
运算符“”X(“n”)
否则(S 包含文字运算符模板),L 被视为以下形式的调用
运算符 "" X <'c1', 'c2', ... 'ck'>()
其中 n 是源字符序列 c1c2...ck。
这表示,如果存在 1(无符号 long long 参数),则 13_xx 应调用 1,否则,13_xx 应调用 2。从 13.5.8 开始,
特别是,它们像普通函数和函数模板一样被查找 并且它们遵循相同的重载解析规则。
根据我的理解,如果 1 不存在,则 13_xx 可以隐式转换为 double 并调用 3。
因此,如果 1 不存在,则 2 和 2 都可以。 3 从标准描述来看在某种程度上是有效的。
我希望有人能帮我解答我的疑惑。非常感谢。
I am a little confused about overloading rules,
let's say there are following literal operators,
unsigned long long operator "" _xx(unsigned long long cooked_literal_int); //1
unsigned long long operator "" _xx(const char * raw_literal_string); // 2
unsigned long long operator "" _xx(long double cooked_literal_double); // 3
if both 1, 2, & 3 are defined, the overloading is obvious,
13_xx //call 1
13.5_xx //call 3
if 1 & 2 are defined,
13_xx //call 1
13.5_xx //call 2
if 2 & 3 are defined
13_xx // call 2 or 3??
13.5_xx // call 3
The confusion comes from latest c++0x standard n3225 2.14.8/3,
If L is a user-defined-integer-literal, let n be the literal without its ud-suffix. If S contains a literal operator with parameter type unsigned long long, the literal L is treated as a call of the form
operator "" X (n ULL)
Otherwise, S shall contain a raw literal operator or a literal operator template (13.5.8) but not both. If S contains a raw literal operator, the literal L is treated as a call of the form
operator "" X ("n")
Otherwise (S contains a literal operator template), L is treated as a call of the form
operator "" X <’c1’, ’c2’, ... ’ck’>()
where n is the source character sequence c1c2...ck.
This says that, if 1 is present (an unsigned long long parameter), 13_xx shall call 1, otherwise, 13_xx shall call 2. And from 13.5.8,
In particular, they are looked up like ordinary functions and function templates
and they follow the same overload resolution rules.
From my understanding, if 1 is not present, 13_xx can be implicitly converted to double and call 3.
Therefore if 1 is not present, both 2 & 3 are somehow valid from the standard description.
I hope someone can help me clear my doubts. Many thanks.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我相信 13.5.8/7 澄清了这个问题:
根据我的理解,常规重载解析规则仅在通过用户定义的文字在隐式调用外部调用时才隐含文字运算符。
所以我认为如果 2 &定义了 3 个,
13_xx
调用 2(原始文字运算符)。I believe that 13.5.8/7 clarifies this issue :
From my understanding, regular overload resolution rules are only implied for literal operators when called outside an implicit invocation through user-defined literals.
So I think that if 2 & 3 are defined,
13_xx
calls 2 (the raw literal operator).