How does the following code work in C++? Is it logical?
const int &ref = 9;
const int &another_ref = ref + 6;
Why does C++ allow l
Constant references can be initialized with literals and temporaries because you can trivially transform them to explicit variables:
int const& ans = 42;
// tranformed:
int __internal_unique_name = 42;
int const& ans = __internal_unique_name;
Or when the lifetime is not extended, such as a function parameter:
f("foobar");
// transformed:
{
string __internal_unique_name = "foobar";
f(__internal_unique_name);
}
(Notice the explicit block in this case.)
While it's possible to do something similar in the non-constant case, that's just not allowed in currenct C++. C++0x (the next standard), however, will have r-value references.
If it wasn't clear, ref + 6 from your code creates a temporary object, which you can visualize as:
int const& ref = int(9);
int const& another_ref = int(ref + 6);
// transformed:
int __A = 9;
int const& ref = __A;
int __B = ref + 6;
int const& another_ref = __B;
This may help you understand/visualize what is happening, but you should not write real code like this. Also I've used double underscore names to illustrate those names are implementation details (used by the compiler) and should not be used by you. Any name which contains adjacent underscores is reserved to the C++ implementation.