How to implement full specialization templates member function within partial specialization templates class

Asked By: Anonymous

Suppose I have a template class Foo with two template args and one template member function.

I want to make member function specialization while the template class is partial specialization, but the following code compiled failed by g++


    template <typename A, typename B>
    class Foo 
    {
    public:
        template<typename C> void bar();
    };
    
    template<> template<typename T> inline void Foo<T, int>::bar<float>() {
    }
    
    int main()
    {
        Foo<double, int> foo;
        foo.bar<float>();
    
        return 0;
    
    }

g++ says:

error: expected initializer before ‘<’ token

template<> template inline void Foo<T, int>::bar() {

I’ve tried

template<typename T> template<> inline void Foo<T, int>::bar<float>() {

and g++ complains more

Can it be possible and how to make it ?


Solution

Answered By: Anonymous

[temp.expl.spec]/17:

In an explicit specialization declaration for a member of a class template or a member template that appears in namespace scope, the member template and some of its enclosing class templates may remain unspecialized, except that the declaration shall not explicitly specialize a class member template if its enclosing class templates are not explicitly specialized as well. In such an explicit specialization declaration, the keyword template followed by a template-parameter-list shall be provided instead of the template<> preceding the explicit specialization declaration of the member. The types of the template-parameters in the template-parameter-list shall be the same as those specified in the primary template definition.

In an explicit specialization declaration for a member of a class template or a member template that appears in namespace scope, the member template and some of its enclosing class templates may remain unspecialized, except that the declaration shall not explicitly specialize a class member template if its enclosing class templates are not explicitly specialized as well.

for your code:

template<typename A, typename B>
class Foo{
public:
    template<typename C> void bar();
};
    
template<typename T> // for `Foo`
template<>           // for `bar`
inline void Foo<T, int>::bar<float>(){}
    
int main(){
    Foo<double, int> foo;
    foo.bar<float>();
    return 0;
}

it’s ill-formed because bar is explicitly specialized but Foo is NOT.

techinplanet staff

techinplanet staff


Windows 10 Kaufen Windows 10 Pro Office 2019 Kaufen Office 365 Lizenz Windows 10 Home Lizenz Office 2019 Home Business Kaufen windows office 365 satın al follower kaufen instagram follower kaufen porno