I was recently asked during a C++ job interview what are the types of riN
variables in the code below:
int val = 25;
int foo() { return val; }
int& foo1() { return val; }
//warning: type qualifiers ignored on function return type
/*const*/ int foo2() { return val; }
const int& foo3() { return val; }
int main()
{
auto ri = foo();
auto ri1 = foo1();
auto ri2 = foo2();
auto ri3 = foo3();
//cannot bind non-const lvalue reference of type 'int&' to an rvalue of type 'int'
//auto& ri4 = foo();
auto& ri5 = foo1();
//cannot bind non-const lvalue reference of type 'int&' to an rvalue of type 'int'
//auto& ri6 = foo2();
auto& ri7 = foo3();
auto&& ri8 = foo();
auto&& ri9 = foo1();
auto&& ri10 = foo2();
auto&& ri11 = foo3();
return 0;
}
auto
ignores the type qualifiers and references, so looks like the types are simply int
, int&
and int&&
.