Hello, I want to specialize following member function:
class foo {
template<typename T>
T get() const;
};
To other class bar
that depends on templates as well.
For example, I would like bar
to be std::pair
with some template parameters, something like that:
template<>
std::pair<T1,T2> foo::get() const
{
T1 x=...;
T2 y=...;
return std::pair<T1,T2>(x,y);
}
Where T1 and T2 are templates as well. How can this be done? As far as I know it should be possible.
So now I can call:
some_foo.get<std::pair<int,double> >();
The full/final answer:
template<typename T> struct traits;
class foo {
template<typename T>
T get() const
{
return traits<T>::get(*this);
}
};
template<typename T>
struct traits {
static T get(foo &f)
{
return f.get<T>();
}
};
template<typename T1,typename T2>
struct traits<std::pair<T1,T2> > {
static std::pair<T1,T2> get(foo &f)
{
T1 x=...;
T2 y=...;
return std::make_pair(x,y);
}
};