Ditto 4.13.1
 
Loading...
Searching...
No Matches
misc_utils.hpp
1#ifndef DITTO_MISC_UTILS_H
2#define DITTO_MISC_UTILS_H
3
4// Properly implementing the (move/copy) assignment operators is tricky/subtle,
5// and there is actually a safer pattern to achieve this:
6// 1. list all the ownership-sensitive fields, especially those playing a
7// role w.r.t. explicit `~Type()` dtor glue;
8// 2. implement some `(Type &, Type &)` `swap`ping function which swaps each of
9// these fields
10// (often _via_ `std::swap()`);
11// 3. write the assignment ctors using the following pattern, or by directly
12// invoking this macro.
13// Notes:
14// - `MoveOrCopy` is expected to be `&& other` or `const& other`,
15// - `move_or_copy` is then expected to be `std::move(other)` or `other`,
16// respectively.
17// NOLINTBEGIN
18#define DEFINE_ASSIGNMENT_OPERATOR_USING_SWAP(Type, MoveOrCopy, move_or_copy, \
19 swap_function) \
20 Type &Type::operator=(Type MoveOrCopy) { \
21 { \
22 if (this == &other) { \
23 return *this; \
24 } \
25 Type temporary{move_or_copy}; \
26 swap_function(other, *this); \
27 } \
28 \
29 return *this; \
30 } \
31 static_assert(true, "") // <- require semicolon after the macro invocation.
32// NOLINTEND
33#endif