Is it a bug that Microsoft VS C++ compiler can Initialize a reference from a temporary object [duplicate]

断了今生、忘了曾经 提交于 2019-12-01 05:09:01

问题


Possible Duplicate:
Binding temporary to a lvalue reference

With VS2008 C++ compiler, the codes are compiled without compile error.

class A{};

int main(){
    A& a_ref = A(); 
    return 0;
}

I believe the C++ standard, both C++2003 and coming C++0x, disallow it. And I also get a compile time error with gcc compiler.

So what I want to know is, is this a known bug for VS compiler to allow initializing reference from a temporary object.

Or is it a feature extension of VS compiler? If yes, what's life cycle of the temporary object?


回答1:


It is the extension.This link explains it. What if we take out the const



来源:https://stackoverflow.com/questions/6501014/is-it-a-bug-that-microsoft-vs-c-compiler-can-initialize-a-reference-from-a-tem

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!