Ditto 4.7.4
Loading...
Searching...
No Matches
misc_utils.hpp
1#ifndef _MISC_UTILS_
2#define _MISC_UTILS_
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#define DEFINE_ASSIGNMENT_OPERATOR_USING_SWAP(Type, MoveOrCopy, move_or_copy, \
18 swap_function) \
19 Type &Type::operator=(Type MoveOrCopy) { \
20 { \
21 Type temporary{move_or_copy}; \
22 swap_function(other, *this); \
23 } \
24 \
25 return *this; \
26 } \
27 static_assert(true, "") // <- require semicolon after the macro invocation.
28
29#endif // _MISC_UTILS_