本文介绍了在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); //1unsigned long long operator "" _xx(const char * raw_literal_string); // 2unsigned long long operator "" _xx(long double cooked_literal_double); // 3 3,则重载很明显,13_xx //call 113.5_xx //call 3如果1& 2,if 1 & 2 are defined, 13_xx //call 113.5_xx //call 2如果2& 3 if 2 & 3 are defined13_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, 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 ()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,根据我的理解,如果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 :根据我的理解,只有当通过用户定义的字面调用 隐式调用之后,常规的重载解析规则才会用于文字运算符。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中的用户定义文字的重载规则的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!
10-30 08:51