dimanche 2 février 2020

How to check if a type is a smart pointer or a reference to a smart pointer

I am trying to implement a structure that checks if a given type is a smart pointer or a reference to a smart pointer at compile time.

I rewrote this solution (that does not work for references):

template<typename T, typename Enable = void>
struct IsSmartPointer : std::false_type {
};

template<typename T>
struct IsSmartPointer<T,
        typename std::enable_if<
                std::is_same<
                        typename std::decay_t<T>, std::unique_ptr<typename std::decay_t<T>::element_type>
                >::value
        >
> : std::true_type {
};

template<typename T>
struct IsSmartPointer<T,
        typename std::enable_if<
                std::is_same<
                        typename std::decay_t<T>, std::shared_ptr<typename std::decay_t<T>::element_type>
                >::value
        >
> : std::true_type {
};

template<typename T>
struct IsSmartPointer<T,
        typename std::enable_if<
                std::is_same<
                        typename std::decay_t<T>, std::weak_ptr<typename std::decay_t<T>::element_type>
                >::value
        >
> : std::true_type {
};

I feel this implementation is very close to the right solution. However, the code below prints zeros:

std::cout << IsSmartPointer<int>::value                          << '\n'
          << IsSmartPointer<const std::shared_ptr<int> &>::value << '\n'
          << IsSmartPointer<std::shared_ptr<int> &>::value       << '\n'
          << IsSmartPointer<const std::shared_ptr<int>>::value   << '\n'
          << IsSmartPointer<std::shared_ptr<int>>::value
          << std::endl;

Could you try to find a mistake because I have run out of ideas?

Aucun commentaire:

Enregistrer un commentaire