“精心设计的类型”是指typedef错误当试图与一个typedef的朋友 [英] "elaborated type refers to typedef" error when trying to befriend a typedef

查看:325
本文介绍了“精心设计的类型”是指typedef错误当试图与一个typedef的朋友的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

假设我有下面的代码(一个简单的CRTP类层次结构)。我想要typedef基类类型来保存自己打字(在我的实际代码,我使用基类类型不止一次,基类接受几个模板参数),我需要支持基类,因为我想保持

Let's say I have the following piece of code (a simple CRTP class hierarchy). I want to typedef the base class type to save myself typing (in my actual code, I use the base class type more than once and the base class takes several template parameters), and I need to befriend the base class since I want to keep the implementation private.

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;

clang说:

[...]/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;

如何解决这个问题?我注意到,我可以简单地输出整个事情只是为了朋友类声明,它的工作正常,但即使一个小的重复的代码,使我感到一点点不舒服,所以我在寻找一个更优雅的正确解决方案。

How do I fix this? I noticed that I could simply type out the whole thing just for the friend class declaration and it works fine, but even a tiny bit of duplicated code makes me feel a tad uncomfortable, so I'm looking for a more elegant "proper" solution.

推荐答案

我相信这不可能与C ++ 03,但添加到C ++ 11 ,其中您可以简单地省略 class 关键字:

I believe that this is not possible with C++03, but was added to C++11 in which you can simply omit the class keyword:

friend BaseType;

这篇关于“精心设计的类型”是指typedef错误当试图与一个typedef的朋友的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆