Home > Net >  How to check if a parameter pack contain all elements of other paraments pack
How to check if a parameter pack contain all elements of other paraments pack

Time:06-14

Example: I have function A with some default arguments, and I want a function that take all the arguments of that function A to check with all arguments I giving. If function A contains all that arguments, then it will call function A with that arguments

Here my sample code:

void A(int a = 0, float b = 0.f, double c = 0.0, const char* d = "") {}

template<typename T1, typename...Arg1, typename...Arg2>
void compareArguments(T1(*func)(Arg1...), Arg2...args)
{
    // some code here:
    if (Arg1... contain Arg2...)  // some thing to check
        func(args...);      // call function.
}

int main()
{
   compareArguments(A, 69, 5.5f);
}

Any idle?

CodePudding user response:

As state in comment, when passing T1(*func)(Arg1...), you lose default parameters.

So instead of passing function pointer, you might pass functor:

[](auto... args) -> decltype(A(args...)){ return A(args...); }

and then std::is_invocable might be used:

template<typename F, typename... Ts>
void call(F func, Ts...args)
{
    if constexpr (std::is_invocable_v<F, Ts...>) {
        func(args...);
    }
}

with usage

call([](auto... args) -> decltype(A(args...)){ return A(args...); }, 69, 5.5f);
  • Related