How to determine when a C++11 move is synthesized?

Jonathan Wakely
Wed Jan 6 23:33:00 GMT 2016

On 6 January 2016 at 21:37, Jeffrey Walton wrote:
> We have a library that's C++03, and its moving towards full support
> for C++11. We are supplementing classes with:
>     class Foo
>     {
>         public:
>             Foo();
>             Foo(const Foo&);
>             Foo& operator=(const Foo&);
> #if defined(CXX11_AVAILABLE)
>             Foo(const Foo&&);
> #endif

I hope that const is just a typo.

>     };
> We have a lot of classes (1500 or so), and its going to take some time
> to evaluate all of them. As a starting point, we would like to
> identify where the compiler is synthesizing a move and then provide it
> (especially for some fundamental/critical base classes).
> How can I determine when a C++11 move is synthesized?

By understanding C++ ;-)

In most cases you shouldn't need to care.

The language rules mean that a move constructor or move assignment
operator will only be implicitly defined for types with:

- no user-declared copy constructor
- no user-declared copy assignment operator
- no user-declared move constructor / move assignment operator
- no user-declared destructor

If you are happy for the compiler to define your copy constructor,
copy assignment and destructor, then you should probably be happy for
it to define moves too.

If you've defined any of those explicitly, because for example the
type manages memory explicitly or does something clever in its
destructor, then the compiler will not generate any moves.

So you should not need to care. If moving might not be safe, it won't
happen. If moving would be safe then it might happen, but in those
cases you are probably better off letting the compiler generate a
correct move rather than interfere.

More information about the Gcc-help mailing list