A question for the C ++ standards guru - what the current standard says about such code, which is fully compiled by both VC ++ and GCC :
#include <iostream> #include <iomanip> using namespace std; class Test { public: Test() { cout << "Test()" << endl; } Test(int x):val_(x){ cout << "Test(" << x << ")" << endl; } Test(const Test& t):val_(t.val_) { cout << "Test(const Test& " << t.val_ << ")" << endl; } Test& operator = (const Test& t) { cout << "Test& operator = (const Test& " << t.val_ <<")" << endl; val_ = t.val_; return *this;} ~Test() { cout << "~Test()" << endl; } int val() const { return val_; } private: int val_ = 0; }; int main(int argc, const char * argv[]) { Test t = t; cout << t.val() << endl; } It turns out that we essentially create an object without initializing the fields, performing only copying the object into itself, i.e. garbage in its place?
val_(t.val_). Why they won't prohibit it - I don’t know, probably because it’s the corner case and there’s no point in fooling around with it. - ixSci