简体   繁体   中英

Partial Specialization for default_delete

I would like to specialize default_delete<_Ty> for all objects derived off of MyBaseClass. This was my best attempt:

template <typename T>
struct default_delete<typename enable_if<is_base_of<MyBaseClass, T>::value, T>::true_type>
{
    ...
};

The compiler seems to be unable to recognize that my type parameter 'T' is being used, which is understandable given that it is 'downstream' from a 'typename' keyword. Is what I'm trying to accomplish possible?

As 0x499602D2 states in comment, it is not possible without an extra dedicated template parameter. you may use your own deleter as follow:

template <typename T, typename Enable = void>
struct my_default_delete : public std::default_delete<T> {}; // default to std::default_delete<T>

template <typename T>
struct my_default_delete<T, typename std::enable_if<std::is_base_of<MyBaseClass, T>::value>::type>
{
    void operator() (T* ) { /* Your specific implementation */ }
};

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