Skip to main content
Add update based on comments
Source Link

Updated: this convention is outdated and OP's original code did it better.

The convention for swap, instead of declaring it friend, is to provide a public member function swap as well as specializing std::swap for your type.

namespace std {
    template<> void swap<Widget>(Widget& a, Widget& b) 
    {
        a.swap(b); 
    }
}

Your class is templated and you can't partially specialize in std yourself. So you would have to provide a non-member swap function outside your class in your namespace.

template<typename T> void swap(unique_ptr<T>& a, unique_ptr<T>& b) 
{
    a.swap(b); 
}

Reference: Effective C++ 55 Specific Ways to Improve Your Programs and Designs, Item 25

The convention for swap, instead of declaring it friend, is to provide a public member function swap as well as specializing std::swap for your type.

namespace std {
    template<> void swap<Widget>(Widget& a, Widget& b) 
    {
        a.swap(b); 
    }
}

Your class is templated and you can't partially specialize in std yourself. So you would have to provide a non-member swap function outside your class in your namespace.

template<typename T> void swap(unique_ptr<T>& a, unique_ptr<T>& b) 
{
    a.swap(b); 
}

Reference: Effective C++ 55 Specific Ways to Improve Your Programs and Designs, Item 25

Updated: this convention is outdated and OP's original code did it better.

The convention for swap, instead of declaring it friend, is to provide a public member function swap as well as specializing std::swap for your type.

namespace std {
    template<> void swap<Widget>(Widget& a, Widget& b) 
    {
        a.swap(b); 
    }
}

Your class is templated and you can't partially specialize in std yourself. So you would have to provide a non-member swap function outside your class in your namespace.

template<typename T> void swap(unique_ptr<T>& a, unique_ptr<T>& b) 
{
    a.swap(b); 
}

Reference: Effective C++ 55 Specific Ways to Improve Your Programs and Designs, Item 25

added 372 characters in body
Source Link

The convention for swap, instead of declaring it friend, is to provide a public member function swap as well as specializing std::swap for your type.

namespace std {
    template<> void swap<Widget>(unique_ptr&Widget& a, unique_ptr&Widget& b) 
    {
        a.swap(b); 
    }
}

Your class is templated and you can't partially specialize in std yourself. So you would have to provide a non-member swap function outside your class in your namespace.

template<typename T> void swap(unique_ptr<T>& a, unique_ptr<T>& b) 
{
    a.swap(b); 
}

Reference: Effective C++ 55 Specific Ways to Improve Your Programs and Designs, Item 25

The convention for swap, instead of declaring it friend, is to provide a public member function swap as well as specializing std::swap for your type.

namespace std {
    template<> void swap<Widget>(unique_ptr& a, unique_ptr& b) 
    {
        a.swap(b); 
    }
}

The convention for swap, instead of declaring it friend, is to provide a public member function swap as well as specializing std::swap for your type.

namespace std {
    template<> void swap<Widget>(Widget& a, Widget& b) 
    {
        a.swap(b); 
    }
}

Your class is templated and you can't partially specialize in std yourself. So you would have to provide a non-member swap function outside your class in your namespace.

template<typename T> void swap(unique_ptr<T>& a, unique_ptr<T>& b) 
{
    a.swap(b); 
}

Reference: Effective C++ 55 Specific Ways to Improve Your Programs and Designs, Item 25

Source Link

The convention for swap, instead of declaring it friend, is to provide a public member function swap as well as specializing std::swap for your type.

namespace std {
    template<> void swap<Widget>(unique_ptr& a, unique_ptr& b) 
    {
        a.swap(b); 
    }
}