template specialization of template class

扶醉桌前 提交于 2019-12-01 04:08:37

You can't partially specialize function templates, sorry but those are the rules. You can do something like:

class foo {
   ...
};


template<typename T>
struct getter {
  static T get(const foo& some_foo);
};

template<typename T1, typename T2>
struct getter< std::pair<T1, T2> > {
static std::pair<T1, T2> get(const foo& some_foo) {
    T1 t1 = ...;
    T2 t2 = ...;
    return std::make_pair(t1, t2);
};

and then call it like

getter<std::pair<int, double> >::get(some_foo);

though. You may have to do some messing around with friendship or visibility if get really needed to be a member function.

To elaborate on sbi's suggestion:

class foo {
   ...
   template<typename T>
   T get() const;
};

template<typename T>
T foo::get() const
{
  return getter<T>::get(*this);
  /*            ^-- specialization happens here */
}

And now you're back to being able to say

std::pair<int,double> p = some_foo.get<std::pair<int, double> >();

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

template <T, V> std::pair<T, V> 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 <T> std::<vector <T> 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.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!