Overloading rules for User-defined-literals in c++0x

邮差的信 提交于 2019-12-24 00:32:52

问题


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.


回答1:


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).



来源:https://stackoverflow.com/questions/4385297/overloading-rules-for-user-defined-literals-in-c0x

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!