XTE_RETURNS()

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

Deduplicates the repetition required for defining the noexcept specifier, requires clause, and body of a function template.
(i.e. eliminates annoying typing when defining forwarding function templates.)


Definitions

#returns
#define XTE_RETURNS(...)                \
	noexcept(noexcept(__VA_ARGS__))     \
	-> decltype(auto)                   \
	requires(requires { __VA_ARGS__; }) \
	{ return (__VA_ARGS__); }

#returns-if
#define XTE_RETURNS_IF(COND, THEN, ...) \
	noexcept(/* ... */)                 \
	-> decltype(auto)                   \
	requires(/* ... */)                 \
	{ /* if constexpr (COND) { (THEN); } return (__VA_ARGS__); */ }
COND is explicitly cast to bool.

#returns-choose
#define XTE_RETURNS_CHOOSE(COND, THEN, ...) \
	noexcept(/* ... */)                     \
	-> decltype(auto)                       \
	requires(/* ... */)                     \
	{ /* if constexpr (COND) { return (THEN); } else { return (__VA_ARGS__); } */ }
COND is explicitly cast to bool. THEN may optionally be wrapped in parentheses.

#returns-first
#define XTE_RETURNS_FIRST(...)               \
	noexcept(/* ... */)                      \
	-> decltype(auto)                        \
	requires(/* requires { ...; } || ... */) \
	{ /* if constexpr (requires { ...; }) { return (...); } else ... */ }
Selects the first valid expression.



Example

auto add(auto a, auto b) XTE_RETURNS(a + b)

struct S {
	void operator+(S) {} // not marked `noexcept`
};

static_assert(noexcept(add(1, 2)));
static_assert(!noexcept(add(S(), S())));
[View in Compiler Explorer]