假设我有以下代码(一个简单的 CRTP 类层次结构)。我想对基类类型进行 typedef 以节省自己的输入(在我的实际代码中,我不止一次使用基类类型,并且基类需要几个模板参数),并且我需要与基类成为朋友,因为我想保留实现私有。
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;
铿锵声 说:
[...]/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;
我该如何解决?我注意到我可以简单地为朋友类声明输入整个内容并且它工作正常,但即使是一点点重复的代码也会让我感到有点不舒服,所以我正在寻找一个更优雅的“正确”解决方案.