Partial specialization of member function [duplicate]

大城市里の小女人 提交于 2019-12-19 19:44:35

问题


Possible Duplicate:
“invalid use of incomplete type” error with partial template specialization

Why is it that I can do this:

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

template <>
void A<int>::foo(int)
{
}

but not this:

template <typename> struct C {};

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

template <typename T>
void A<C<T> >::foo(int)
{
}

For the second case, GCC gives the following error:

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> >'

EDIT:

When explaining why the second example is not allowed, please also consider that making the member function also a template has no effect on which example works and which does not. That is, this still works:

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

template <>
template <typename U>
void A<int>::foo(U)
{
}

but this does not:

template <typename> struct C {};

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

template <typename T>
template <typename U>
void A<C<T> >::foo(U)
{
}

So the reason cannot be that function templates can only be fully specialized, because the third example is not a full specialization (the template parameter U is still there), and yet it works.


回答1:


Function templates can only be specialised completely, not partially.

You're using the fact that member functions of class templates are themselves function templates, so this rule still applies.


As for your edit: The following things can be explicitly (i.e. completely) specialized, from 14.7.3/1:

An explicit specialization of any of the following:

— function template

— class template

member function of a class template

— static data member of a class template

— member class of a class template

— member enumeration of a class template

— member class template of a class or class template

member function template of a class or class template

can be declared by a declaration introduced by template<>;

I've emphasized the two statements that apply to your case. Absent any other explicit provisions, those entities can not be specialized partially.



来源:https://stackoverflow.com/questions/12335762/partial-specialization-of-member-function

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!