【发布时间】:2016-11-12 09:56:42
【问题描述】:
#include <iostream>
using namespace std;
struct S {
int m_i;
};
int main() {
S s1;
// okay - implicit copy constructor
S s2(s1);
S s3;
// okay - implicit copy assignment
s3 = s1;
// awkward
if(s1 == s2)
cout << "can't be" << endl;
return 0;
}
这篇文章没有按预期编译,并且考虑到这个设计决策的年代和(可能)依赖于它的代码量,我们永远坚持下去。不过,是否有人对其背后的最初原因有预感?
【问题讨论】:
-
因为比较是基于值的运算,而不是位运算。
-
有建议将 memberwise 与 C++ 进行比较,例如 this one。可能成为未来标准的一部分。