Robust
Robust

Reputation: 67

specialized function of a specialized class

Can someone please explain me why is this not compiling...the specialized function of a specialized class ??? In the specialized version of the templated class the specialized function is not compiling.

        #include<iostream>
        using namespace std;

    //Default template class
        template<typename T>
        class X
        {
        public:
            void func(T t) const;
        };


        template<typename T>
        void X<T>::func(T b) const
        {
            cout << endl << "Default Version" << endl;
        }


//Specialized version        
        template<>
        class X<int>
        {
            public:
            void func(int y) const;
        };

        template<>
        void X<int>::func(int y)
        {
            cout << endl << "Int Version" << endl;
        }

        int main()
        {
            return 0;
        }

Upvotes: 1

Views: 137

Answers (2)

Andy Prowl
Andy Prowl

Reputation: 126432

An explicit specialization of a class template is a concrete class, not a template, so you can (or rather, should) just write:

// template<>
// ^^^^^^^^^^
// You should not write this

void X<int>::func(int y) const
//                       ^^^^^
//                       And do not forget this!
{
    cout << endl << "Int Version" << endl;
}

Thus leaving out the template<> part.

Also, mind the fact that your func() function is const-qualified in the declaration - so you have to use the const qualifier even in the defintion.

Here is a live example.

Upvotes: 3

Scott Jones
Scott Jones

Reputation: 2908

I think you left off the trailing const modifier

Upvotes: 1

Related Questions