vendredi 5 février 2021

enable_if not triggering in the constructor?

I am trying to implement very basic shared_ptr (most functions skipped in question for simplicity), and for the case we have a pointer of base and create a derived pointer I created a separate constructor.

template<typename T>
class shared_ptr {
public:
    shared_ptr(T* resource) :pointee_(resource),
        refCount_(new size_t(1))
    {
        std::cout << typeid(resource).name();
        deleter_ = [=]() {delete resource; };
    }

    template<typename U, std::enable_if_t<std::is_base_of_v<T, U>>>
    shared_ptr(U* resource) :pointee_(resource),
        refCount_(new size_t(1))
    {
        std::cout << typeid(resource).name();
        deleter_ = [=]() {delete resource; };
    }

    ~shared_ptr()
    {
        --(*refCount_);
        if (!(*refCount_)) {
            deleter_();
        }
    }
private:
    size_t* refCount_;
    T* pointee_;
    std::function<void()> deleter_;
};

class A { int a; };
class B :public A {};

int main() {
    shared_ptr<A> ptr(new B);
}

Here, although the is_base_of is 1 (I tried separately), the enable_if is not triggered and conductor is not enabled. Whereas when I remove the enable if, the overload resolution works and the constructor is enabled.
Can anyone hint what am I missing? I don't need conceptual suggestions, just a hint what am I doing wrong or how can I enable this constructor for only class hierarchies?

P.S. I know that I can remove enable_if and compiler will warn me in case types are not convertible.

Aucun commentaire:

Enregistrer un commentaire