VC编译器与GCC编译在引用上的不同?
class A {};
void f(A &a) {}
int main() {
A& a = A(); //GNU GCC 编译错误
f(A()); //GNU GCC 编译错误
return 0;
}
GCC 编译报错 error: invalid initialization of non-const reference of type 'A&' from a temporary of type 'A'|
c++程序设计语言 书中也提到过这个问题。
const引用才允许临时对象赋值给引用的。
void f(const A &a) {}
const A& a = A(); 这样才是被允许的做法
VC内部做了什么优化吗,还是我VC设置的问题。怎么让VC也报出这个错?