【问题标题】:How do I define a template function within a template class outside of the class definition?如何在类定义之外的模板类中定义模板函数?
【发布时间】:2011-02-24 17:39:06
【问题描述】:

给定:

template <class T>
class Foo
{
public:
    template <class U>
    void bar();
};

如何在类定义之外实现 bar,同时仍然可以访问模板参数 T 和 U?

【问题讨论】:

    标签: c++ templates


    【解决方案1】:

    IIRC:

    template<class T> template <class U>
    void Foo<T>::bar() { ...
    

    【讨论】:

    • 我还是不明白为什么template&lt;class T, class U&gt; void Foo&lt;T&gt;::bar(){...} 不起作用...
    • @Xeo 它有两个独立的模板,类和函数。 (我认为这就是原因)
    • @Xeo 因为它是Foo&lt;T&gt;::bar&lt;U&gt;,而不是Foo&lt;T,U&gt;::bar
    猜你喜欢
    • 1970-01-01
    • 2023-03-13
    • 1970-01-01
    • 1970-01-01
    • 2022-07-22
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-08-09
    相关资源
    最近更新 更多