scx scx - 2 years ago 47
C++ Question

Triggering void constexpr at compile time?

I have a

function that groups many
for design contracts. I would like to call it at compile time, without having to create an unused

Here is an example of what I currently have to do (c++17).

template<size_t N = 0, typename... Ts, typename F>
inline constexpr int tuple_for(const std::tuple<Ts...>& t, const F& func) {

if constexpr(N < sizeof...(Ts) - 1) {
return tuple_for<N + 1, Ts...>(t, func);
} else {
return 0;

auto do_checks = [](const auto& t) {
"You need some_method");
return 0;

constexpr int i_am_sad = tuple_for(my_tuple, do_checks);

Is there any other way to achieve this behaviour? Maybe something new in c++17?

Thank you.

Note that as these checks are to be generalized, I believe having the asserts in a function is the right way to go here.

Answer Source

You might use it in other constexpr context as static_assert:

static_assert((static_cast<void>(tuple_for(my_tuple, do_checks)), true), "!");

[Note]: the cast to void is to generalize, if you want to return class with evil overload comma.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download