模板类的模板专业化 [英] template specialization of template class
问题描述
我想要专门处理以下成员函数:
class foo {
template< typename T&
T get()const;
};
到其他类 bar
以及。
例如,我想 bar
为 std :: pair
有一些模板参数,如下:
模板<
std :: pair< T1,T2> foo :: get()const
{
T1 x = ...;
T2 y = ...;
return std :: pair< T1,T2>(x,y);
}
其中T1和T2也是模板。如何做到这一点?据我所知,应该是
可能。
现在我可以调用:
some_foo.get< std :: pair< int,double> >();
完整/最终答案:
模板< typename T>结构特征
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);
}
};
你不能部分地专门化函数模板,对不起,这些都是规则。您可以执行以下操作:
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);
};
,然后调用
getter< std :: pair< int,double> > :: get(some_foo);
如果 get
真的需要成为一个成员函数,你可能必须用 friend
/ p>
要详细说明sbi的建议:
class foo {
...
template< typename T>
T get()const;
};
template< typename T>
T foo :: get()const
{
return getter< T> :: get(* this);
/ * ^ - 专业化发生在这里* /
}
你回到能够说
std :: pair< int,double> p = some_foo.get< std :: pair< int,double> >();
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);
}
};
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 friend
ship 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> >();
这篇关于模板类的模板专业化的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!