如何在不同模板类的模板类中使用嵌套模板类

How to use a nested template class within a template class from a different template class

本文关键字:嵌套      更新时间:2023-10-16

首先,我希望问题标题有一定的意义。

我有以下类结构:

class A : public Singleton<A>
{
public:
    template <typename T> class Buffer
    {
    public:
         //ctor & dtor
         T* get() { return ptr; }
    private:
         T* ptr;
    };
    // class A stuff
};

这个类应该作为模板参数传递给处理程序类,并在类的模板函数中使用:

template <class MODEL> class Handler
{
public:
      // ctor & dtor
      template <typename T> typename MODEL::Buffer<T>* create(...) // error c2988
      { // create a buffer }
};

然而,编译器无法确定正确的类型名,并引发错误C2988:无法识别的模板声明/定义(vc++2012年11月VS 2012中的CTP编译器)。我找不到任何解决方案来告诉编译器如何处理内部类模板。所以问题是:如何让它发挥作用?

如有任何帮助,我们将不胜感激。

您需要帮助编译器消除歧义,并告诉它Buffer是什么:

template <typename T> typename MODEL::template Buffer<T>* create(...);
//                                    ^^^^^^^^

请参阅此问答;A了解更多信息。