0
votes

With const, as indicated by the comment, msvc 11 and g++ 4.7.0 refuse to compile this:

#include <memory>       // std::unique_ptr
#include <utility>      // std::move
using namespace std;

struct CommandLineArgs
{
    typedef unique_ptr<
        wchar_t const* const [],
        void(*)( wchar_t const* const* )
        > PointerArray;

    //PointerArray const  args;         // Oops
    PointerArray        args;
    int const           count;

    static wchar_t const* const* parsed(
        wchar_t const       commandLine[],
        int&                count
        )
    {
        return 0;
    }

    static void deallocate( wchar_t const* const* const p )
    {
    }

    CommandLineArgs(
        wchar_t const   commandLine[]   = L"",
        int             _               = 0
        )
        : args( parsed( commandLine, _ ), &deallocate )
        , count( _ )
    {}

    CommandLineArgs( CommandLineArgs&& other )
        : args( move( other.args ) )
        , count( move( other.count ) )
    {}
};

int main()
{}

The error messages do not seem to be particularly informative, but here's g++'s output:

main.cpp: In constructor 'CommandLineArgs::CommandLineArgs(CommandLineArgs&&)':
main.cpp:38:38: error: use of deleted function 'std::unique_ptr::unique_ptr(const std::unique_ptr&) [w
ith _Tp = const wchar_t* const; _Dp = void (*)(const wchar_t* const*); std::unique_ptr = std::unique_ptr]'
In file included from c:\program files (x86)\mingw\bin\../lib/gcc/mingw32/4.7.0/include/c++/memory:86:0,
                 from main.cpp:1:
c:\program files (x86)\mingw\bin\../lib/gcc/mingw32/4.7.0/include/c++/bits/unique_ptr.h:402:7: error: declared here

Why?

2
-1, OP didn't put an effort to make it SSCE, if CommandLineArgs would contain only const member and move ctor, the issue would be obvious.Abyx
@Abyx: I told you in chat that you don't have to submit ever more evidence. it's enough. please let it rest: it hurts me (on your behalf). ok, explanation for you: the error message is produced when the class only contains const members. what does that tell you, if anything?Cheers and hth. - Alf
here is a SSCE - ideone.com/VOlcA , compiler error clearly says what's wrong there.Abyx
@Abyx: your point seems to be that with different code producing a different error message, that different question would not be worth asking. i agree.Cheers and hth. - Alf
actually it's the same code, reduced to the point where error message is way more clear. There are only meaningful lines, and std::move replaced with its (expected) implementation. Then we see that such std::move can't be used there, and we understand why it falls back to copying.Abyx

2 Answers

6
votes

You can not move const object. The error is because of your move constructor.

The unique_ptr, has deleted copy constructor and move constructor declared as :

unique_ptr( const unique_ptr & other );
unique_ptr( unique_ptr && other );

Since your unique_ptr is declated const, it picks copy constructor, not move constructor.

1
votes

No copy c-tor that have signature unique_ptr(const unique_ptr&);