为什么类模板
问题描述:
我的问题是w.r.t以下螺纹的显式实例之前,需要外部类模板的显式实例:specialize a member template without specializing its parent为什么类模板
我对这个标准说,这是非法的这样做精绝。但我想了解为什么这样做是非法的?如果允许,会有什么影响?
答
的也许是因为这样的事情:
template <typename T>
struct foo
{
template <typename U>
struct bar
{
typedef U type;
};
};
template <typename T>
struct foo<T>::bar<int> // imaginary
{
typedef void type;
};
template <>
struct foo<float>
{
template <typename U>
struct bar
{
typedef U* type;
};
};
// is it void [foo<T>::bar<int>] or
// int* [foo<float>::bar<U>]?
typedef foo<float>::bar<int>::type ambiguous;
一个明智的解决办法是说:“我们会让整个事情明确的”。
+1。甚至可能是'foo'根本没有内部类型的'bar'。 –
2010-09-17 08:01:40
这个答案引发了一个问题,但为什么部分专门化'bar' *是被允许的:'template template struct foo :: bar {typedef void type; };对于需要*两个*参数的'bar'来说都没问题。现在,如果你做'foo :: bar :: type',你会得到相同的“歧义”:) –
2010-09-17 09:51:17
@Johannes:哼,那我就给你吧。 :) 这就是我得到的。 – GManNickG 2010-09-17 14:38:44