gonzo
gonzo

Reputation: 519

C++: Static assert that a parameter pack matches a function type

Given the function type void(), void(int), and so on..., can we static_assert() that Args&&... args is compatible with the function type?

template <typename F = void(), typename... Args>
auto call(Args&&... args)
{
    // TODO: check if F is compatible with Args

    if constexpr (std::is_same_v<void, decltype(func(args...))>)
        func(std::forward<Args>(args)...);
    else
        return func(std::forward<Args>(args)...);
}

Using decltype(func(args...)) will give an error, but it's not the best error. Also, is it possible that the static_assert() will make it worse by hiding the two types from the compiler output?

Upvotes: 1

Views: 425

Answers (1)

max66
max66

Reputation: 66200

You have tagged C++17, so... what about using std::is_invocable?

I suppose something as follows

template <typename F = void(), typename... Args>
auto call (Args && ... args)
 {
   static_assert( std::is_invocable_v<F, Args...> );

   // ...
 }

Upvotes: 3

Related Questions