template specialization of template class

前端 未结 2 1908
星月不相逢
星月不相逢 2021-01-12 10:56

I want to specialize following member function:

class foo {
    template
    T get() const;
};

To other class bar

2条回答
  •  没有蜡笔的小新
    2021-01-12 11:30

    You need to overload your member function for pair, like in

    template  std::pair foo::get()
    

    In the general case you will need to be able to disambiguate between the various overloads. In the case disambiguation is easy because pair is templated on 2 types while the original member was templated on T only.

    If instead you needed a specialization for, e.g., std::vector, that is for a container with a single parameter template, you have to be careful since given it can be confusing for the compiler to understand if you wish to instantiate the template specialization where the template T is std::vector or the specialization for the overload,

    template  std:: foo::get() const 
    

    Your proposed syntax cannot work since you are completely specializing the member function,

    template <>,

    but you are leaving out two unspecified types, T1 and T2.

提交回复
热议问题