假设我
template <typename T>
class F;
可以创建另一个模板,该模板采用 F
作为类型,具有默认值和专门化。
template <typename S>
class G;
template <>
template <typename T>
class G <F<T> >
{
};
我可以实例化 G > g_of_f。编译器正确选择了 G 的特化,一切都很好。
所以这是我的问题。我想使用列表中的多个模板类型名来执行此操作。但是当我
template <typename U, typename S>
class H;
template <typename U>
template <typename T>
class H <U, F<T> >
{
};
现在尝试时,我无法实例化 H; > h_of_f
,因为编译器选择原始模板 H
而不是特化。
我在 g++ 4.1 和 g++ 4.4 中观察到了相同的行为。
G
和 H
之间有什么区别,导致编译器无法按我期望的方式工作?
Let's say I have
template <typename T>
class F;
I can create another template who takes an F
as a type, with a default and a specialization.
template <typename S>
class G;
template <>
template <typename T>
class G <F<T> >
{
};
I can instantiate G<F<int> > g_of_f
. The compiler correctly selects the specialization of G
, and everything is great.
So here's my problem. I want to do this with more than one template typename in the list. But when I try
template <typename U, typename S>
class H;
template <typename U>
template <typename T>
class H <U, F<T> >
{
};
now I cannot instantiate H<void, F<int> > h_of_f
, because the compiler is selecting the original template H
and not the specialization.
I've observed the same behavior with g++ 4.1 and g++ 4.4.
What is the difference between G
and H
that's preventing the compiler from working the way I'm expecting?
发布评论
评论(2)
此语法:
用于在模板之外专门化/定义模板成员:
http://www.comeaucomputing.com/techtalk/templates/#outsidedef
This syntax:
is for specializing/defining template members outside of template:
http://www.comeaucomputing.com/techtalk/templates/#outsidedef
template<>
语法用于引入显式特化声明,这里有部分特化:The
template<>
syntax is used to introduce an explicit specialization declaration, what you have here are partial specializations :