加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

c – 如何在模板中使用嵌套的typedef?

发布时间:2020-12-16 03:28:46 所属栏目:百科 来源:网络整理
导读:我想从模板类型Base派生一个类型Test,我专门研究派生类型(即Base Test). 在模板化类型中,我想使用派生类型(模板参数)中定义的typedef. 但是,我收到此编译错误: error C2039: 'X' : is not a member of 'Test' 这是代码片段: template typename Tclass Base
我想从模板类型Base派生一个类型Test,我专门研究派生类型(即Base< Test>).

在模板化类型中,我想使用派生类型(模板参数)中定义的typedef.

但是,我收到此编译错误:

error C2039: 'X' : is not a member of 'Test'

这是代码片段:

template <typename T>
class Base
{
protected:
  void func(typename T::X x) {}
};


class Test : public Base<Test>
{
public:
  typedef int X;
};

这是可行的,如果是这样,我需要做什么修复?

(我看到了这类问题的几个答案,但看起来我的方案没有通过前缀typename来修复 – 它是否与从派生类型专用的模板派生有关?)

解决方法

这对我有用:
template <typename T> struct Traits;

template <typename Derived>
class Base
{
   protected:
      void func(typename Traits<Derived>::X x) {}
};


class Test;

template <> struct Traits<Test>
{
  typedef int X;
};


class Test : public Base<Test>
{
};

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读