Let's say we have member class with two member functions defined as follows:
class SomeClass
{
private:
int val = {};
public:
const int getVarLRef() & {
return val;
}
const int getVarCLRef() const& {
return val;
}
};
int main()
{
auto var1 = SomeClass().getVarCLRef();
auto var2 = SomeClass().getVarLRef();
return 0;
}
I not quite understand what is the difference between const&
and &
.
Why it works with getVarCLRef
if we specified this function as const&
? Shouldn't it be allowed to be invoked only with lvalues?
getVarLRef
, on the other hand, works just fine and fails to compile in this case as expected.
I use C++11 and gcc 7.3.0