模板类的变量,其模板类模板参数设置为派生模板的基础模板,并带有该变量。

13

我试图创建一个派生类(普通模板),它有一个模板类型的变量,该模板类型以其模板类参数为基类(普通模板,与派生类具有相同的参数)的类型。但是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++呢?


1
对我来说编译完全没有问题,使用的是VS 2010 SP1。 - Xeo
奇怪,也许我需要更新。检查了我的示例代码,它明显无法编译。我就知道这里有什么问题! - user173342
@user:你使用的是哪个版本的VS? - Xeo
10
我仍然很难读懂标题。 - Dennis Zickefoose
2
+1 为今天最佳标题!:D - Nim
显示剩余4条评论
1个回答

4

derivOfDoom<> 中不合格使用 innocuousBase 会被解释为 innocuousBase<VT>,就像在该上下文中不合格使用 derivOfDoom 将被解释为 derivOfDoom<VT> 一样。我不记得这是否符合标准行为,但解决方法很简单:完全限定 innocuousBase,这样编译器就知道你指的是 innocuousBase 类模板而不是 innocuousBase<VT> 基类:

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

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接