Are temporary objects in C++ const indeed?
问题 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: function_returning_object().some_non_const_method(); is valid for C++ compiler. And it makes me wonder - are temporary objects in C++ const indeed? If yes, then why the code above is considered correct by the compiler? 回答1: No, they're not. Not unless you declare the return type as const. 回答2: It depends. int f(); const int g();