I have a constexpr
function that groups many static_asserts
for design contracts. I would like to call it at compile time, without having to create an unused constexpr
variable.
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) {
func(std::get<N>(t));
if constexpr(N < sizeof...(Ts) - 1) {
return tuple_for<N + 1, Ts...>(t, func);
} else {
return 0;
}
}
auto do_checks = [](const auto& t) {
static_assert(has_some_method_v<decltype(t)>,
"You need some_method");
return 0;
}
[[maybe_unused]]
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.
edit: Note that as these checks are to be generalized, I believe having the asserts in a function is the right way to go here.
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 overloaded comma.