#include <iostream>
using namespace std;
int main()
{
// Case 1
cout << &5; // This line fails to compile. Ok, I get this because "5" is a rvalue.
// Case 2
const int& ref = 5;
cout << &ref; // Works fine. Why?
// Case 3
cout << &"SomeString"; // Works fine. Ok, I get this because "SomeString" is a lvalue
return 0;
}
Why case 1 fails and case 2 passes? I could not find concrete explanation elsewhere. Most answers were confusing and self contradictory.
I understand that "5" in integer literal and it is a rvalue (we cannot take address of it) Then what special thing a const lvalue ref in case 2 doing, that it allows to take address of "5" ? As I have read, a reference (be it lvalue or rvalue) is just an alias. It has no address of its own.