c++ - Why does "most important const" have to be const? -
in http://herbsutter.com/2008/01/01/gotw-88-a-candidate-for-the-most-important-const/ mentions "most important const" c++ deliberately specifies binding temporary object reference const on stack lengthens lifetime of temporary lifetime of reference itself. wondering why c++ allows lifetime of object lengthened when reference const , not when isn't? rational behind feature , why have const?
here's example:
void square(int &x) { x = x * x; } int main() { float f = 3.0f; square(f); std::cout << f << '\n'; }
if temporaries bind non-const lvalue references, above happily compile, produce rather surprising results (an output of 3
instead of 9
).
Comments
Post a Comment