dimanche 15 mai 2022

Question about operator==( const std::shared_ptr& lhs, const std::shared_ptr& rhs )

As per the document, which says that:

template < class T, class U > 
bool operator==( const
std::shared_ptr<T>& lhs, const std::shared_ptr<U>& rhs ) noexcept;

It indicates lhs and rhs could be different types. What surprises me is that the code snippet below does not compile. I fully understand what the compiler complains. What confuses me is the said document says the lhs and rhs could be different types.

Here is the said code snippet:

#include<memory>
#include<vector>
#include<iostream>

struct Widget
{
    std::vector<int> vec;
    int var;
};

int main()
{
    auto wp{std::make_shared<Widget>(std::vector<int>{1,2,3}, 69)};

    std::shared_ptr<std::vector<int>> vp{wp, &wp->vec};

    std::cout << wp.owner_before(vp) << std::endl;
    std::cout << vp.owner_before(wp) << std::endl;

    std::cout << (wp==vp) << std::endl;
}

Aucun commentaire:

Enregistrer un commentaire