extern class 声明用法是否符合 C++ 标准?

Is extern class declaration usage C++ standard compliant?

class My_Container
{
    /*...*/
    class iterator
    {
        extern class const_iterator;
        Node* _ptr;
    public:
        bool operator==(const iterator& other) {return _ptr == other._ptr;}
        bool operator==(const const_iterator& other) {return _ptr == other._ptr;}
    };
    class const_iterator
    {
        Node* _ptr;
    public:
        bool operator==(const const_iterator& other) {return _ptr == other._ptr;}
        bool operator==(const iterator& other) {return _ptr == other._ptr;}
    };
}

如果我省略 extern class const_iterator; 声明,它不会编译。 它符合 C++ 17 标准吗? 内部 class 可以访问另一个内部 class 的私有成员吗?

Is extern class declaration usage C++ standard compliant?

不,这不符合标准。 类 不能有存储 class 说明符。

Can an inner class access to private members of another inner class?

与其他方式相同class;它可以访问 public 成员。该示例访问 class 之外的私有成员,因此它是 ill-formed.

It doesn't compile if I omit extern class const_iterator; declaration.

改用常规的前向声明:

class const_iterator;
class iterator
{
   // ...
};
class const_iterator
{
   // ...
};

我建议用另一种方式解决问题:首先定义 const_iterator。使 iterator 可隐式转换为 const_iterator。使 const_iterator 仅与 const_iterator.

具有可比性