为什么我可以在私有类型上使用auto?

问题描述 投票:130回答:4

以下代码编译并运行时,我感到很惊讶(vc2012&gcc4.7.2)

class Foo {
    struct Bar { int i; };
public:
    Bar Baz() { return Bar(); }
};

int main() {
    Foo f;
    // Foo::Bar b = f.Baz();  // error
    auto b = f.Baz();         // ok
    std::cout << b.i;
}

这段代码编译得好吗?为什么这是正确的?为什么我可以在私有类型上使用auto,而我不能使用它的名字(如预期的那样)?

c++ c++11 auto private-members
4个回答
110
投票

auto的规则在很大程度上与模板类型推导相同。发布的示例与您可以将私有类型的对象传递给模板函数的原因相同:

template <typename T>
void fun(T t) {}

int main() {
    Foo f;
    fun(f.Baz());         // ok
}

为什么我们可以将私有类型的对象传递给模板函数?因为只有该类型的名称不可访问。类型本身仍然可用,这就是为什么你可以将它返回到客户端代码。


106
投票

访问控制适用于名称。与标准中的此示例相比:

class A {
  class B { };
public:
  typedef B BB;
};

void f() {
  A::BB x; // OK, typedef name A::BB is public
  A::B y; // access error, A::B is private
}

12
投票

寒冷和R. Martinho Fernandes已经很好地回答了这个问题。

我只是不能错过用哈利波特类比回答问题的机会:

class Wizard
{
private:
    class LordVoldemort
    {
        void avada_kedavra()
        {
            // scary stuff
        }
    };
public:
    using HeWhoMustNotBeNamed = LordVoldemort;

    friend class Harry;
};

class Harry : Wizard
{
public:
    Wizard::LordVoldemort;
};

int main()
{
    Wizard::HeWhoMustNotBeNamed tom; // OK
    // Wizard::LordVoldemort not_allowed; // Not OK
    Harry::LordVoldemort im_not_scared; // OK
    return 0;
}

https://ideone.com/I5q7gw

感谢Quentin提醒我Harry的漏洞。


8
投票

为了增加其他(好的)答案,这里有一个来自C ++ 98的例子,说明问题实际上与auto完全没有关系

class Foo {
  struct Bar { int i; };
public:
  Bar Baz() { return Bar(); }
  void Qaz(Bar) {}
};

int main() {
  Foo f;
  f.Qaz(f.Baz()); // Ok
  // Foo::Bar x = f.Baz();
  // f.Qaz(x);
  // Error: error: ‘struct Foo::Bar’ is private
}

不禁止使用私有类型,它只是命名类型。例如,在所有版本的C ++中创建该类型的未命名临时表是可以的。

© www.soinside.com 2019 - 2024. All rights reserved.