Why struct with auto_ptr and explicit destructor unable to swap

Viewed 128

look at this code. Test struct has a auto_ptr and explicit destructor. When I build this code in Windows environment (Visual Studio 2017 professional) with error level 4, it shows this warning.

warning C4239: nonstandard extension used: 'argument': conversion from 'Test' to 'Test &' note: A non-const reference may only be bound to an lvalue; copy constructor takes a reference to non-const

What I understand is std::swap accept the reference for the Test class and unable to cast instance to reference. If I remove destructor or auto_ptr warning disappear. Any idea what is the cause?

#include <algorithm>
#include <memory>
typedef struct Test
{
public:
    int a;
    std::auto_ptr<Test> b;
    ~Test()
    {
    }
} Test_Type;


int main()
{
    Test_Type arr[2];
    arr[0].a = 5;
    arr[1].a = 3;
    std::swap(arr[0], arr[1]);
}
1 Answers
Related