考虑以下代码:
class A
{
private:
class B {};
public:
B f();
};
A a;
A::B g()
{
return a.f();
}
编译器拒绝这个 - g 不能返回 A::B 因为 A::B 是私有的。
但是假设我现在使用 decltype 来指定 g 的返回值:
class A
{
private:
class B {};
public:
B f();
};
A a;
decltype(a.f()) g()
{
return a.f();
}
突然之间它编译得很好(使用 g++ >= 4.4)。
所以我基本上使用 decltype 来绕过访问说明符,这是我在 C++98 中无法做到的。
这是故意的吗?这是好习惯吗?