Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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?

like image 886
mrn Avatar asked Nov 30 '22 09:11

mrn


1 Answers

No, they're not. Not unless you declare the return type as const.

like image 115
Benjamin Lindley Avatar answered Dec 05 '22 07:12

Benjamin Lindley