具有模板类模板参数的模板类的变量设置为具有该变量的派生模板的基模板

Variable of a template class with a template class template parameter set to a base template of the derived template with the variable

本文关键字:变量 派生 设置 参数      更新时间:2023-10-16

我正在尝试创建一个派生类(普通模板),它有一个模板类型的变量,该变量的模板类参数为派生类(带变量的)的基类类型(普通模板,与派生类的参数相同)。这让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++?

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

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