Why use the template <> without specialization?

I read the STL source code (which turned out to be funny and very useful) and I came across this thing

//file backwards/auto_ptr.h, but also found on many others.

template<typename _Tp>                                                                                                 
      class auto_ptr

//Question is about this:
template<>
    class auto_ptr<void>

Is a part added template<>to avoid class duplication?

+3
source share
1 answer

This specialization. For instance:

template <typename T>
struct is_void
{
    static const bool value = false;
};

This template would have is_void<T>::valuelike falsefor any type that is obviously wrong. What you can do is use this syntax to say: "I fill in T myself and specialize":

template <> // I'm gonna make a type specifically
struct is_void<void> // and that type is void
{
    static const bool value = true; // and now I can change it however I want
};

is_void<T>::value false, , T void. , true.

, auto_ptr. void. , , .

, , void auto_ptr .

+7

Source: https://habr.com/ru/post/1733635/


All Articles