Drake
drake_copyable.h
Go to the documentation of this file.
1 #pragma once
2 
3 // ============================================================================
4 // N.B. The spelling of the macro names between doc/Doxyfile_CXX.in and this
5 // file must be kept in sync!
6 // ============================================================================
7 
8 /** @file
9 Provides careful macros to selectively enable or disable the special member
10 functions for copy-construction, copy-assignment, move-construction, and
11 move-assignment.
12 
13 http://en.cppreference.com/w/cpp/language/member_functions#Special_member_functions
14 
15 When enabled via these macros, the `= default` implementation is provided.
16 Code that needs custom copy or move functions should not use these macros.
17 */
18 
19 /** DRAKE_NO_COPY_NO_MOVE_NO_ASSIGN deletes the special member functions for
20 copy-construction, copy-assignment, move-construction, and move-assignment.
21 Drake's Doxygen is customized to render the deletions in detail, with
22 appropriate comments. Invoke this this macro in the public section of the
23 class declaration, e.g.:
24 <pre>
25 class Foo {
26  public:
27  DRAKE_NO_COPY_NO_MOVE_NO_ASSIGN(Foo)
28 
29  // ...
30 };
31 </pre>
32 */
33 #define DRAKE_NO_COPY_NO_MOVE_NO_ASSIGN(Classname) \
34  Classname(const Classname&) = delete; \
35  void operator=(const Classname&) = delete; \
36  Classname(Classname&&) = delete; \
37  void operator=(Classname&&) = delete;
38 
39 /** DRAKE_DEFAULT_COPY_AND_MOVE_AND_ASSIGN defaults the special member
40 functions for copy-construction, copy-assignment, move-construction, and
41 move-assignment. This macro should be used only when copy-construction and
42 copy-assignment defaults are well-formed. Note that the defaulted move
43 functions could conceivably still be ill-formed, in which case they will
44 effectively not be declared or used -- but because the copy constructor exists
45 the type will still be MoveConstructible. Drake's Doxygen is customized to
46 render the functions in detail, with appropriate comments. Invoke this this
47 macro in the public section of the class declaration, e.g.:
48 <pre>
49 class Foo {
50  public:
51  DRAKE_DEFAULT_COPY_AND_MOVE_AND_ASSIGN(Foo)
52 
53  // ...
54 };
55 </pre>
56 */
57 #define DRAKE_DEFAULT_COPY_AND_MOVE_AND_ASSIGN(Classname) \
58  Classname(const Classname&) = default; \
59  Classname& operator=(const Classname&) = default; \
60  Classname(Classname&&) = default; \
61  Classname& operator=(Classname&&) = default; \
62  /* Fails at compile-time if default-copy doesn't work. */ \
63  static void DRAKE_COPYABLE_DEMAND_COPY_CAN_COMPILE() { \
64  (void) static_cast<Classname& (Classname::*)( \
65  const Classname&)>(&Classname::operator=); \
66  }