【发布时间】:2010-12-09 14:17:54
【问题描述】:
这是一个说明问题的最小代码示例:
#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 编译器发出以下警告:
"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,但没有得到诊断。为什么这里需要访问复制构造函数?除了使对象可复制(不在我的控制范围内)或使显式副本通过(当现实生活中的对象复制成本很高时)之外,我如何消除警告?
【问题讨论】:
-
您是否真的在某个地方的类实现中使用了该复制构造函数?
-
没有。我已经发布了生成诊断的整个代码文件。
-
仅供参考:我也尝试使用 VC++ 2005 和 2008,编译时没有警告。所以看起来你是对的,问题似乎是特定于 IBM 编译器的
标签: c++ compiler-warnings aix