Using std::less<> for unique_ptr to raw pointer comparison fails to compile, but works with wrapper?

Neither std::unique_ptr<int> nor int involve user-defined types; in terms of ADL their associated namespaces are namespace std only.

When you use a wrapper type you have an associated namespace of the root namespace (the namespace where you declared the wrapper), which means that ADL can find the free operator<s defined there.

Indeed, this is a Good Thing since it prevents someone else writing their own operator<(int*, std::unique_ptr<int> const&) which might have a different behavior to yours.

CLICK HERE to find out more related problems solutions.

Leave a Comment

Your email address will not be published.

Scroll to Top