const int& dummy = 5;
The code snippet above compiles without any errors / warnings using the g++ Compiler
. Conversely using the code snippet
int& dummy = 5;
produces the following error code:
invalid initialization of non-const reference of type ‘int&’ from an rvalue of type ‘int’ int& dummy = 5;
Since the reference type from c++ is a lvalue reference I find it surprising that it is possible to assign an rvalue to it if is declared as const. Why?