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?
CommandLineArgs
would contain only const member and move ctor, the issue would be obvious. – Abyxstd::move
replaced with its (expected) implementation. Then we see that suchstd::move
can't be used there, and we understand why it falls back to copying. – Abyx