简体   繁体   中英

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!

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

另外,您可能希望将size的类型更改为unsigned intsize_t (假设size不能为负)。

您需要将int size添加到第一个列表中-也就是说,编译器没有size来源-您没有将其声明为模板参数,并且它不是常量。

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM