Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Making sure copy constructor is never called when move constructor exists

When designing a class that can be moved but not copied, it's natural to declare the copy constructor as private. When having only movable and trivially copyable objects as instance members, it makes sense to allow the compiler to implicitly generate the move constructor.

However, when supporting both VS11 and G++4.7 I found an incompatibility:

  • VS11 requires explicitly defined move constructors
  • G++ requires explicit move constructors to have either matching public copy constructor or the noexcept keyword.
  • VS11 doesn't support the noexcept keyword.

As you can see, this places me in a bit of a pickle. My class must not be copied. I must support both VS11 and MinGW / GCC. I need my class to be movable.

Have I misunderstood something, or is there a way around this tiny problem? Can I make compilation fail if a call to the copy constructor is generated? Any better solution to this problem?

like image 576
Max Avatar asked Oct 07 '22 06:10

Max


1 Answers

If you add this to the source file

#ifdef _MSC_VER
#define noexcept
#endif

You will be able to define it as noexcept on GCC but VC++ will ignore noexcept.

like image 101
Dirk Holsopple Avatar answered Oct 10 '22 04:10

Dirk Holsopple