Overload resolution with higher order variational functions

Suppose I have a higher order variational function

template<typename F, typename ...Args>
void execution(F func, Args&&... args)
{
    func(std::forward<Args>(args)...);
}

Then for this overload set

void f() {}
void f(int arg) {}

Overload resolution will not be possible

int main()
{
    execution(f, 1);
    execution(f);
    return 0;
}

However, if only one of the two is provided, the program compiles

  • Why is this happening? Why does the template argument fail?
  • If I remove f()from the set and replace it with f(arg, arg2), the problem. Is there a workaround, or I should always provide the type of function as the template argument?

    execution<void()>(f);
    
+4
source share
3 answers

. , f:

struct f_overload_set {
  template<typename...As>
  auto operator()(As&&...as)->
    decltype(f(std::declval<As>()...))
  { return f(std::forward<As>(as)...); }
};

f_overload_set{} template .

template , ++ , . f , . , .

+3

, . . , ,

template<typename Func> void execution(Func func);

void f();
void f(int);

execution(f);

, .

, , :

template<typename... Args>
 void execution(void (*func)(Args ...), Args ... args)
{
  func(std::forward<Args>(args) ...);
}
+8

'f':

:

template<typename F>
void execution(F func)
{}

void f() {}
void f(int arg) {}

int main()
{
    execution(f);
    return 0;
}

'f', /

+1

Source: https://habr.com/ru/post/1539158/


All Articles