Bug
Bug

Reputation: 495

partial class templates specialization, c++

I'm trying to figure it out, so I have this attempt of specializing a class template:

template <class T, int size> class arr{
public:
    arr(T* in):_in(in){};
    void print()
    {
        for (int i = 0; i < size; i++)
        {
            cout << _in[i];
        }
        cout << endl;
    }
private:
    T* _in;
};

I want to create a special template for arr<char>. Before I added int size as a type parameter it was easy and the class declaretion was template<> class arr<double>.

Now I have this template <> class arr<double,size> which results in this error:

template argument 2 is invalid

Any ideas of how to do it? thanks!

Upvotes: 2

Views: 449

Answers (2)

Praetorian
Praetorian

Reputation: 109089

template <int size> class arr<char, size>

Also, you may want to change the type for size to an unsigned int or size_t (assuming size cannot be negative).

Upvotes: 4

Puppy
Puppy

Reputation: 146910

You need to add int size to the first list- that is, the compiler has no source for size- you didn't declare it as a template argument and it's not a constant.

template<int size> class arr<double, size> { ... }

Upvotes: 0

Related Questions