Answer the question
In order to leave comments, you need to log in
Why can non-const constructors bind & to rvalue?
https://godbolt.org/z/3Te8KzMr7
#include <iostream>
class MyClass {
public:
~MyClass(void);
MyClass(void);
MyClass(MyClass&);
//MyClass(MyClass const&);
};
MyClass::~MyClass(void) { std::cout << this << " des" << std::endl; }
MyClass::MyClass(void) { std::cout << this << " void" << std::endl; }
MyClass::MyClass(MyClass& ref) { std::cout << this << " &" << std::endl; }
//MyClass::MyClass (MyClass const & cref){std::cout << this << " const &"<< std::endl;}
MyClass f(void) {
MyClass c;
return c;
}
int main() {
//MyClass & ncref = f();// cannot bind non-const lvalue reference of type 'int&' to an rvalue of type 'int'
MyClass c1 = f();// и msvc, и gcc не выдают предупреждений
MyClass c2 ( f() ); // msvc компилирует и запускает, но выдаёт ошибку об отсутствии подходящего конструктора, gcc нет
return 0;
}
Answer the question
In order to leave comments, you need to log in
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question