开发者

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<> cl开发者_如何学编程ass 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!


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).


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> { ... }
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜