在c ++ 0x中的用户定义文字的重载规则 [英] Overloading rules for User-defined-literals in c++0x

查看:147
本文介绍了在c ++ 0x中的用户定义文字的重载规则的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我对重载规则有些困惑,

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

3,则重载很明显,

13_xx //call 1
13.5_xx //call 3

如果1& 2,

if 1 & 2 are defined,

13_xx //call 1
13.5_xx //call 2

如果2& 3

if 2 & 3 are defined

13_xx // call 2 or 3??
13.5_xx // call 3

混乱来自最新的c ++ 0x标准n3225 2.14.8 / 3,

The confusion comes from latest c++0x standard n3225 2.14.8/3,


如果L是用户定义的整数文字,则n是不带其ud后缀的文字。如果S包含参数类型为unsigned long long的文本运算符,则文字L将被视为

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

operatorX(n ULL)

operator "" X (n ULL)

否则,S应包含一个原始字面量运算符或文字运算符模板(13.5.8),但不能同时包含两者。如果S包含一个原始的字面量运算符,字面量L被视为一个形式的调用

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

operatorX(n)

operator "" X ("n")

否则(S包含文字运算符模板),L被视为

Otherwise (S contains a literal operator template), L is treated as a call of the form

operatorX < ','c2',...'ck'>()

operator "" X <’c1’, ’c2’, ... ’ck’>()

其中n是源字符序列c1c2 ... ck。

where n is the source character sequence c1c2...ck.

这说明,如果1存在(无符号长整数参数),13_xx将调用1,否则13_xx将调用2.从13.5.8,

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,


特别是,它们像普通函数和函数模板
一样被查找,并且它们遵循相同的重载解析规则。 p>

In particular, they are looked up like ordinary functions and function templates and they follow the same overload resolution rules.

根据我的理解,如果1不存在,13_xx可隐式转换为double并调用3.

From my understanding, if 1 is not present, 13_xx can be implicitly converted to double and call 3.

因此,如果1不存在,则2& 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.

推荐答案

我相信13.5.8 / 7说明了这个问题:

I believe that 13.5.8/7 clarifies this issue :


注意:文字运算符和文字
运算符模板通常通过用户定义的
文字(2.14.8)隐式调用
。但是,除了
上述约束
,它们
是普通命名空间范围函数
和函数模板。特别是,
他们被查找为普通的
函数和函数模板,
他们遵循相同的重载
解析规则。

Note: literal operators and literal operator templates are usually invoked implicitly through user-defined literals (2.14.8). However, except for the constraints described above, they are ordinary namespace-scope functions and function templates. In particular, they are looked up like ordinary functions and function templates and they follow the same overload resolution rules.

根据我的理解,只有当通过用户定义的字面调用 隐式调用之后,常规的重载解析规则才会用于文字运算符。

From my understanding, regular overload resolution rules are only implied for literal operators when called outside an implicit invocation through user-defined literals.

所以我认为如果2& 3, 13_xx 调用2(原始字面量操作符)。

So I think that if 2 & 3 are defined, 13_xx calls 2 (the raw literal operator).

这篇关于在c ++ 0x中的用户定义文字的重载规则的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆