尝试与typedef成为朋友时,"elaborated type指的是typedef"错误

jay*_*ica 7 c++ templates typedef

假设我有以下代码(简单的CRTP类层次结构).我想键入基类类型来保存自己输入(在我的实际代码中,我多次使用基类类型,基类需要几个模板参数),我需要与基类成为朋友,因为我想保留私有的实施.

template< class D >
class Base
{

public:

    void foo() { *static_cast< D * >(this)->foo_i(); }

};

template< class T >
class Derived : public Base< Derived< T > >
{

public:

    typedef class Base< Derived< T > > BaseType;

private:

    // This here is the offending line 
    friend class BaseType;

    void foo_i() { std::cout << "foo\n"; }

};

Derived< int > crash_dummy;
Run Code Online (Sandbox Code Playgroud)

铿说:

[...]/main.cpp:38:22: error: elaborated type refers to a typedef
    friend class BaseType;
             ^
[...]/main.cpp:33:44: note: declared here
    typedef class Base< Derived< T > > BaseType;
Run Code Online (Sandbox Code Playgroud)

我该如何解决?我注意到我可以简单地为朋友类声明键入整个内容并且它工作正常,但即使是一小部分重复的代码也让我觉得有点不舒服,所以我正在寻找一个更优雅的"正确"解决方案.

Jos*_*eld 11

我相信这对于C++ 03是不可能的,但是被添加到C++ 11中,您可以在其中省略class关键字:

friend BaseType;
Run Code Online (Sandbox Code Playgroud)