0

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.

scx
  • 3,221
  • 1
  • 19
  • 37

2 Answers2

2

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.

Jarod42
  • 203,559
  • 14
  • 181
  • 302
  • Thanks, though not visually pleasing, this is prefect. What is the evil comma overload? Sounds interesting. – scx May 22 '17 at 21:34
  • Oh my! That is so evil... I love it :D Made a little macro helper, looks pretty clean. – scx May 22 '17 at 21:45
0

static_assert is a declaration-statement, so you can just plonk it in free space.

No need for a function.

This is precisely the use case for that. :)

Lightness Races in Orbit
  • 378,754
  • 76
  • 643
  • 1,055