Drake
reinit_after_move< T > Class Template Reference

Type wrapper that performs value-initialization on the wrapped type, and guarantees that when moving from this type that the donor object is reset to its value-initialized value. More...

#include <drake/common/reinit_after_move.h>

Public Member Functions

 reinit_after_move ()
 Constructs a reinit_after_move<T> with a value-initialized wrapped value. More...
 
 reinit_after_move (const T &value)
 Constructs a reinit_after_move<T> with the given wrapped value. More...
 
Implements CopyConstructible, CopyAssignable, MoveConstructible,

MoveAssignable.

 reinit_after_move (const reinit_after_move &)=default
 
reinit_after_moveoperator= (const reinit_after_move &)=default
 
 reinit_after_move (reinit_after_move &&other)
 
reinit_after_moveoperator= (reinit_after_move &&other)
 
Implicit conversion operators to make reinit_after_move<T> to act

as the wrapped type.

 operator T & ()
 
 operator const T & () const
 

Detailed Description

template<typename T>
class drake::reinit_after_move< T >

Type wrapper that performs value-initialization on the wrapped type, and guarantees that when moving from this type that the donor object is reset to its value-initialized value.

Background:

For performance reasons, we often like to provide overloaded move functions on our types, instead of relying on the copy functions. When doing so, it is more robust to rely on the compiler's = default implementation using member-wise move, instead of writing out the operations manually. In general, move functions should reset the donor object of the move to its default-constructed (empty) resource state. Inductively, the member fields' existing move implementations do this already, except in the case of non-class (primitive) members, where the donor object's primitives will not be zeroed. By wrapping primitive members fields with this type, they are guaranteed to be zeroed on construction and after being moved from.

Example:

class Foo {
 public:
  DRAKE_DEFAULT_COPY_AND_MOVE_AND_ASSIGN(Foo)
  Foo() = default;
 private:
  std::vector<int> items_;
  reinit_after_move<int> sum_;
};

When moving from Foo, the donor object will reset to its default state: items_ will be empty and sum_ will be zero. If Foo had not used the reinit_after_move wrapper, the sum_ would remain intact (be copied) while moving, even though items_ was cleared.

Template Parameters
Tmust support CopyConstructible, CopyAssignable, MoveConstructible, and MoveAssignable and must not throw exceptions during construction or assignment.

Constructor & Destructor Documentation

reinit_after_move ( )
inline

Constructs a reinit_after_move<T> with a value-initialized wrapped value.

See http://en.cppreference.com/w/cpp/language/value_initialization.

Here is the caller graph for this function:

reinit_after_move ( const T &  value)
inline

Constructs a reinit_after_move<T> with the given wrapped value.

This is an implicit conversion, so that reinit_after_move<T> behaves more like the unwrapped type.

reinit_after_move ( const reinit_after_move< T > &  )
default
reinit_after_move ( reinit_after_move< T > &&  other)
inline

Member Function Documentation

operator const T & ( ) const
inline
operator T & ( )
inline
reinit_after_move& operator= ( const reinit_after_move< T > &  )
default

Here is the caller graph for this function:

reinit_after_move& operator= ( reinit_after_move< T > &&  other)
inline

The documentation for this class was generated from the following file: