开发者

Overloading rules for User-defined-literals in 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

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 r开发者_开发知识库aw 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.


I believe that 13.5.8/7 clarifies this issue :

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.

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

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜