XTE_CONSTRUCTS()

Defined in header <xte/preproc/constructs.hpp>

Deduplicates the repetition required for defining the body, noexcept specifier, and requires clause of a constructor template.


Definition

#define XTE_CONSTRUCTS(BODY, ...) \
	noexcept(/* ... */)           \
	requires(/* ... */)           \
	: /* ... */                   \
	{ BODY; }
Accepts a constructor body consisting of an expression optionally wrapped in parentheses, followed by a list of things to initialize and what to initialize them with, respectively, in alternating order. Values must be wrapped in two sets of parentheses, unless the intention is to brace-initialize, in which case they must be wrapped in one outer set of parentheses and one inner set of brackets. Wrapping the thing to initialize in one set of parentheses designates it as a base class type, and wrapping it in two sets of parentheses designates it as a pack of base class types. See [Example #1].



Example

template<typename T, typename... Ts>
struct X : T, Ts... {
	int x[2];

	X(int a, int b, auto arg, auto... args) XTE_CONSTRUCTS(, // :
		x,({ a, b }), // x { a, b }
		(T),((arg)), // T(arg)
		((Ts)),((args...)) // Ts(args...)...
	) // {}
};

struct A { int a; };
struct B { double b0, b1; };
struct C { double c0, c1; };
static_assert(noexcept(X<A, B, C>(1, 2, 418, 3.14159, 6.28318)));
// : x { 1, 2 }, A(418), B(3.14159, 6.28318), C(3.14159, 6.28318) {}
[View in Compiler Explorer]