如何定义模板的模板化子类?

时间:2014-02-23 16:34:50

标签: c++

我刚刚更新到GCC 4.8.2(从4.7开始),现在我收到以下代码的警告:

template <class T_base>
class factory {
private:
    template <class T>
    struct allocator : factory {
                    // ^ warning: invalid use of incomplete type 'class factory<T_base>'
    };
};

为避免出现此警告,我尝试在struct allocator之外定义factory,但现在出现以下错误:

template <class T_base>
class factory {
private:
    template <class T>
    struct allocator;
};

template <class T_base, class T>
struct factory<T_base>::allocator<T> : factory<T_base> {
                     // ^ error: too few template-parameter-lists
};

我做错了什么?是否有上述结构的语法可以避免警告和错误?

2 个答案:

答案 0 :(得分:3)

你需要像这样拼写:

template <class T_base>
template <class T>
struct factory<T_base>::allocator : factory<T_base>
{
    // ...
};

答案 1 :(得分:1)

声明嵌套模板的正确语法是有两个单独的模板参数列表:

template <class T_base>
template <class T>
struct factory<T_base>::allocator : factory<T_base> {
};

但是,我在质疑这段代码的语义意义。

相关问题