c++ - 模板类中的模板函数

标签 c++ templates function-templates class-template

我有这个代码:

template <class T>
class MyClass {
public:
    template <class U>
    void foo() {
        U a;
        a.invoke();
    }
};

我想要这种形式:

template <class T>
class MyClass {
public:
    template <class U>
    void foo();
};

template <class T> /* ????? */
void MyClass<T>::foo() {
    U a;
    a.invoke();
}

我该怎么做?正确的语法是什么?

最佳答案

这样写:

template <class T>
template <class U>
void MyClass<T>::foo() { /* ... */ }

关于c++ - 模板类中的模板函数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8640390/

相关文章:

c++ - 基类中的抽象基成员变量

c++ - 通过引用返回的函数与返回 const 值的 const 函数之间的区别

c++ - 将构造函数参数变成模板参数

c++ - 参数相关查找不起作用

c++ - 复杂的运算符重载和模板

c++ - 函数模板参数推导模板参数vs默认模板参数vs返回类型

c++ - 如何从函数模板的实例化中推断出模板参数?

c++ - 将I/O作为参数传递有什么意义?

c++ - 将命名参数用于模板模板参数是否有任何用途

c++ - 模板构造函数重载问题