在C ++ 11中查找运算符的规则 [英] Rules for lookup of operators in C++11

查看:139
本文介绍了在C ++ 11中查找运算符的规则的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

N3337 ,工作草案,标准编程语言C ++,在第13.3.1.2节中给出了以下示例,p。 10:

N3337, "Working Draft, Standard for Programming Language C++," gives the following example in clause 13.3.1.2, p. 10:

struct A { };
void operator + (A, A);
struct B {
  void operator + (B);
  void f ();
};
A a;
void B::f() {
  operator+ (a,a);   // error: global operator hidden by member
  a + a;             // OK: calls global operator+
}

/ p>

However, this is just a note:


注意:表达式中运算符的查找规则与函数调用中的运算符函数名称的查找规则不同,如下所示示例:

Note: The lookup rules for operators in expressions are different than the lookup rules for operator function names in a function call, as shown in the following example:

我的问题是,在标准中,它说,这是什么必须发生,一个例子?

My question is where in the standard does it say that this is what has to happen as opposed to just having the note with an example?

据我所知,根据第13.3.1.2节, 2,运算符表达式转换为运算符函数调用。

As far as I can tell, according to clause 13.3.1.2, p. 2, operator expressions are converted to operator function calls. So why and how should there be a difference in the example above?

在查看问题,我想我可能已经忽略了p。 3和p.6在同一条款中,一起声明全局候选人和成员候选人在查找运算符时被同等地考虑(因此查找规则与注释不同)。然而,我的查询这个主题是由这个例子,以相同的方式编译GCC 4.8和Clang:

After looking into the problem, I think that I may have overlooked p. 3 and p.6 in the same clause that together state that global candidates and member candidates are considered equally when looking up operators (thus lookup rules are different as the note says). However, my inquiry into this subject was stemmed by this example that compiles in the same way with GCC 4.8 and Clang:

struct X {};  struct Y {};

void operator+(X, X) { }
void operator+(X, Y) { }

void test() {
  void operator+(X, X);
  X x; Y y;

  x + x;  // OK
  x + y;  // OK

  operator+(x, y);  // error
  operator+(x, x);  // OK
}

为什么当操作符函数

以下是来自GCC的错误:

Here are the errors from GCC:

operators-main-ss.cpp: In function ‘void test()’:
operators-main-ss.cpp:13:17: error: could not convert ‘y’ from ‘Y’ to ‘X’
   operator+(x, y);  // error
                 ^

此处来自Clang:

operators-main-ss.cpp:13:16: error: no viable conversion from 'Y' to 'X'
  operator+(x, y);  // error
               ^
operators-main-ss.cpp:1:8: note: candidate constructor (the implicit copy constructor) not viable: no
      known conversion from 'Y' to 'const X &' for 1st argument;
struct X {};  struct Y {};
       ^
operators-main-ss.cpp:7:22: note: passing argument to parameter here
  void operator+(X, X);
                     ^

编译器是否正确使块声明影响全局名称但不是其他?

Are the compilers correct to have the block declaration shadow the global name in one case but not the other?

推荐答案


编译器是否正确, $ b全局名在一个case但不是其他?

Are the compilers correct to have the block declaration shadow the global name in one case but not the other?

我得出结论,两个编译器都错了。我相信 x + y; 也应该失败。 13.3.1.2p3清楚地说明:

I have come to the conclusion that both compilers are wrong. I believe x + y; should also fail. 13.3.1.2p3 states it clearly:


非成员候选者的集合是操作符的不合格
查找的结果@在表达式的上下文中根据
通常用于在非限定函数调用中的名称查找规则(3.4.2)
除非所有成员函数都被忽略。

The set of non-member candidates is the result of the unqualified lookup of operator@ in the context of the expression according to the usual rules for name lookup in unqualified function calls (3.4.2) except that all member functions are ignored.

结果, x + y; operator + ,y); Commeau在线会在代码中产生以下错误:

As a result, there should be no difference between x + y; and operator+(x, y); in your example. Commeau online produces the following errors with the code:

"ComeauTest.c", line 11: error: no operator "+" matches these operands
            operand types are: X + Y
    x + y;  // OK
      ^
"ComeauTest.c", line 13: error: no suitable user-defined conversion from "Y"
 to "X"
          exists
    operator+(x, y);  // error

这篇关于在C ++ 11中查找运算符的规则的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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