What are the differences between the following two implementations for accessing a private constant class member?
// Auto& (compile ok)
class Foo {
private:
const int _foo;
public:
Foo(const int& in) : _foo(in) {}
auto& foo() { return _foo; }
}
// Explicit type (compiler error)
class Foo {
private:
const int _foo;
public:
Foo(const int& in) : _foo(in) {}
int& foo() { return _foo; }
}
With auto
the compiler does not complain but the explicit int
type declaration in fact gives compiler error (which is due to the constness). In this case, what is auto
deduced?