【问题标题】:Variable of a template class with a template class template parameter set to a base template of the derived template with the variable带有模板类模板参数的模板类的变量设置为带有变量的派生模板的基本模板
【发布时间】:2011-06-07 20:49:07
【问题描述】:

我正在尝试创建一个派生类(普通模板),它有一个模板类型的变量,该变量的模板类参数是基类的类型(普通模板,与派生类相同的参数)派生类(带有变量的类)。这让 VC++ 对我非常生气,我无法平息它的愤怒。这是一个简单的例子:

template<template<typename VT> class CT, typename VT> struct encapThing {};

template<typename VT> struct innocuousBase {};

template<typename VT> struct derivOfDoom : public innocuousBase<VT>
{
    encapThing<innocuousBase, VT> ohgodhelp; //C3200
};

它会抛出一个 C3200,说它需要一个类模板。现在,我明白为什么这可能会认为模板中存在模板的递归循环,即使 实际上 并非如此。否则我如何说服 VC++?

【问题讨论】:

  • 对我来说编译得非常好,VS 2010 SP1。
  • 奇怪,也许我需要更新。检查了我的示例,它肯定无法编译。我知道这很可疑!
  • @user:你用的是哪个VS版本?
  • 我还在努力阅读标题。
  • +1 获得当天最佳标题! :D

标签: c++ templates


【解决方案1】:

derivOfDoom&lt;&gt; 内部不合格使用innocuousBase 将被解释为innocuousBase&lt;VT&gt;,就像在该上下文中不合格使用derivOfDoom 将被解释为derivOfDoom&lt;VT&gt;。我不记得这是否是符合标准的行为,但解决方法很简单:完全限定innocuousBase,因此编译器知道您指的是innocuousBase 类模板而不是innocuousBase&lt;VT&gt; 基类:

template<typename VT> struct derivOfDoom : innocuousBase<VT>
{
    encapThing<::innocuousBase, VT> ohgodhelp;
};

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2011-08-25
    • 2014-09-08
    • 2011-02-03
    • 1970-01-01
    • 2014-03-13
    • 1970-01-01
    • 2012-01-20
    • 2012-12-07
    相关资源
    最近更新 更多