1

是否可以专门化在模板类中声明的内部类?或多或少类似于以下示例(不起作用):

template<unsigned N>
class Outer {
    struct Inner {
        typedef int Value;
    };
    typedef typename Inner::Value Value;
};

template<>
struct Outer<0>::Inner {
    typedef float Value;
};
4

2 回答 2

3

它可以做到,但不像你尝试的那样。您必须专门化整个外部类型:

template<>
struct Outer<0>
{
    struct Inner {
        typedef float Value;
    };
    typedef float Value;
};
于 2012-05-26T21:25:49.933 回答
0

我发现最好的解决办法是搬到Inner外面去。

template<unsigned N>
struct Inner {
    typedef int Value;
};

template<>
struct Inner<0> {
    typedef float Value;
};

template<unsigned N>
class Outer {
    typedef typename Inner<N>::Value Value;
};
于 2012-05-26T21:35:12.003 回答