c++ - Why won't this code compile? -
with const
, indicated comment, msvc 11 , g++ 4.7.0 refuse 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 not seem particularly informative, 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 c:\program files (x86)\mingw\bin\../lib/gcc/mingw32/4.7.0/include/c++/memory:86:0, 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?
you can not move const object. error because of move constructor.
the unique_ptr, has deleted copy constructor , move constructor declared :
unique_ptr( const unique_ptr & other ); unique_ptr( unique_ptr && other );
since unique_ptr declated const, picks copy constructor, not move constructor.
Comments
Post a Comment