xieite::tmp()

Defined in fragment xieite:tmp
(header-only: <xieite/fn/tmp.hpp>)


Creates a temporary object which lives for the duration of the full expression which contains it.
Can be used for ignoring output parameters (see example).
Does not heap-allocate, unlike std::make_unique<T>(args...).get().


Declaration

template<typename T>
[[nodiscard]] constexpr T& tmp(T&& value = {}) noexcept;


Example

import xieite;

void bad_api(int a, int b, int* out) {
	int sum = a + b;
	*out = sum;
	xieite::dump(sum);
}

int main() {
	// Don't care about output parameter
	bad_api(1, 2, &xieite::tmp<int>());
}
Output:
3