开发者

Explicit specialization, C++

How to write 开发者_JAVA技巧explicit specialization for object

Car<T>

in virtual method clear()?

template <class U>
class List
{
   public:
        virtual void clear();

};


template <class T>
template <>
void List < Car <T> >::clear()   //Specialization U = Car <T>, compiler error
{
    ....
}

class car:

template <class T>
class Car
{
   T speed;
   ...
}

Compile error:

Error 16 error C3855: 'List': template parameter 'Car' is incompatible with the declaration h:...\List.hpp 75 Error 20 error C2264: 'List::clear' : error in function definition or declaration; function not called h:...\List.hpp 75

But this construction is OK

template <>
void List < Car <double> >::clear()   //Specialization U = Car <T>, compiler error
{
    ....
}


I think the only way you could do this is:

template<class T>
class Car
{
};

template <class U>
class List
{
   public:
        virtual void clear();

};

template <class T>
class List<Car<T> >
{
    public:
        virtual void clear() { /* specialization */ }
};

or, non inline version:

template <class T>
class List<Car<T> >
{
    public:
        virtual void clear();
};

template <class T>
void List<Car<T> >::clear() {
   /* specialization */ 
}

since you are not really specializing List<T> but instead you are partially specializing it, given that a template type still appears. My deduction could be wrong, anyway.


This faq item should get you going.

Therefore it is something like this :

struct Car
{
};

template <>
void List < Car >::clear()   //Specialization U = Car <T>, compiler error
{
    ....
}
0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜