为什么从IBM XL C / C ++编译器发出此警告? [英] Why this warning from IBM XL C/C++ compiler?

查看:86
本文介绍了为什么从IBM XL C / C ++编译器发出此警告?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

下面是说明问题的最小代码示例:

Here's a minimum code example that illustrates the problem:

#include <iostream>

class Thing
{
   // Non-copyable
   Thing(const Thing&);
   Thing& operator=(const Thing&);

   int n_;

public:
   Thing(int n) : n_(n) {}

   int getValue() const { return n_;}
};

void show(const Thing& t)
{
   std::cout << t.getValue() << std::endl;
}

int main()
{
   show(3);
}

这会产生相同的错误:

int main()
{
    show( Thing(3) );
}

AIX下的IBM XL C / C ++ 8.0编译器发出以下警告:

IBM XL C/C++ 8.0 compiler under AIX emits these warnings:

"testWarning.cpp", line 24.9: 1540-0306 (W) The "private" copy constructor "Thing(const Thing &)" cannot be accessed.
"testWarning.cpp", line 24.9: 1540-0308 (I) The semantics specify that a temporary object must be constructed.
"testWarning.cpp", line 24.9: 1540-0309 (I) The temporary is not constructed, but the copy constructor must be accessible.

我也尝试了使用 -Wall和 -pedantic的g ++ 4.1.2,但没有得到诊断。为什么在这里需要访问复制构造函数?除了使对象可复制(这超出了我的控制范围)或使显式副本通过(当现实中的对象复制成本很高时)之外,如何消除警告?

I also tried g++ 4.1.2 with "-Wall" and "-pedantic" and got no diagnostic. Why is access to the copy constructor required here? How can I eliminate the warning, besides making the object copyable (which is outside my control) or making an explicit copy to pass (when the real-life object is expensive to copy)?

推荐答案

此规则在标准的§8.5.3/ 5中。确定了三种基本情况。第一个涉及初始化程序(在您的情况下为 3)为左值或具有类类型。由于这两个都不成立,因此您遇到的是第三种情况:使用没有类类型的右值初始化const引用。该情况在8.5.3 / 5中的最后一个项目符号中涉及:

The rules for this are in §8.5.3/5 of the standard. There are three basic situations identified. The first involve the initializer ('3' in your case) being either an lvalue, or having class type. Since neither of those is true, what you have is the third case: initializing a const reference with an rvalue that does not have a class type. This case is covered by the final bullet in 8.5.3/5:


否则,将创建 cv1 T1类型的临时项并从使用非引用副本初始化(8.5)规则​​的初始值设定项表达式。然后将引用绑定到临时目录。如果T1与T2相关,则cv1必须具有与cv2相同的cv资格或更高的cv资格;否则,程序格式不正确。
Otherwise, a temporary of type "cv1 T1" is created and initialized from the initializer expression using the rules for a non-reference copy initialization (8.5). The reference is then bound to the temporary. If T1 is reference-related to T2, cv1 must be the same cv-qualification as, or greater cv-qualification than, cv2; otherwise, the program is ill-formed.

编辑:重读,我认为IBM是正确的。我以前曾想过可能需要复制临时文件,但这不是问题的根源。要使用第8.5节中指定的使用非参考副本初始化来创建临时副本,它需要副本ctor。特别是在这一点上,它等效于以下表达式:

rereading, I think IBM has it right. I was previously thinking of the possibility of having to copy the temporary, but that's not the source of the problem. To create the temporary using non-reference copy initialization as specified in §8.5, it needs the copy ctor. In particular, at this point it's equivalent to an expression like:

T x = a;

基本上等于:

T x = T(a);

T x = T(a);

Ie需要先创建一个临时对象,然后将其复制到正在初始化的对象中(在这种情况下,该对象也是 一个临时对象)。总结所需的过程,它大致等同于以下代码:

I.e. it's required to create a temporary, then copy the temporary to the object being initialized (which, in this case, is also a temporary). To summarize the required process, it's roughly equivalent to code like:

T temp1(3);
T temp2(temp1); // requires copy ctor
show(temp2);    // show's reference parameter binds directly to temp2

这篇关于为什么从IBM XL C / C ++编译器发出此警告?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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