That is because a temporary can not bind to a non-const reference.
double &m = a;
a
is of type int
and is being converted to double
. So a temporary is created. Same is the case for user-defined types as well.
Foo &obj = Foo(); // You will see the same error message.
But in Visual Studio, it works fine because of a compiler extension enabled by default. But GCC will complain.
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…