Are temporary objects in C++ const indeed?

前端 未结 6 1614
走了就别回头了
走了就别回头了 2021-01-06 10:11

I always believed that temporary objects in C++ are automatically considered as const by the compiler. But recently I experienced that the following example of code:

6条回答
  •  星月不相逢
    2021-01-06 11:01

    Temporary objects aren't const, but they can only bind to const lvalue references. It's easy to demonstrate that allowing temporaries to bind to non-const lvalue references would be bade in virtually all scenarios. You also can't take the address of a temporary, even though you can bind a reference to it, and a number of other very silly things happen with regards to temporaries in C++03. Just be glad that C++0x will be here soon... hopefully.

提交回复
热议问题