相关疑难解决方法(0)

成员函数的部分特化

可能重复:
部分模板特化的"无效使用不完整类型"错误

为什么我能做到这一点:

template <typename T>
struct A
{
    void foo(int);
};

template <>
void A<int>::foo(int)
{
}
Run Code Online (Sandbox Code Playgroud)

但不是这个:

template <typename> struct C {};

template <typename T>
struct A
{
    void foo(int);
};

template <typename T>
void A<C<T> >::foo(int)
{
}
Run Code Online (Sandbox Code Playgroud)

对于第二种情况,GCC给出以下错误:

test.cpp:10:23: error: invalid use of incomplete type 'struct A<C<T> >'
test.cpp:4:8: error: declaration of 'struct A<C<T> >'
Run Code Online (Sandbox Code Playgroud)

编辑:

在解释为什么不允许第二个例子时,请同时考虑使成员函数也是一个模板对哪个例子有效,哪个没有影响.也就是说,这仍然有效:

template <typename T>
struct A
{
    template <typename U>
    void foo(U);
};

template <>
template <typename U>
void …
Run Code Online (Sandbox Code Playgroud)

c++ templates partial-specialization template-specialization

5
推荐指数
1
解决办法
4392
查看次数