const reference to temporary oddity
问题 We all know that things like this are valid in c++: const T &x = T(); while: T &x = T(); is not. In a recent question the conversation lead to this rule. The OP had posted some code which clearly evokes UB. But I would have expect a modified version of it to work (This is the modified version): #include <iostream> using namespace std; class A { public: A(int k) { _k = k; }; int get() const { return _k; }; int _k; }; class B { public: B(const A& a) : _a(a) {} void b() { cout << _a.get(); }