C++ overloaded function as template argument

后端 未结 4 682
生来不讨喜
生来不讨喜 2020-12-03 14:40

the simplified version of my code is here

int foo(int x)
{
  return x;
}

int foo(int x, int y)
{
  return x+y;
}

template
int ba         


        
4条回答
  •  北海茫月
    2020-12-03 15:20

    You can give an explicit template argument:

    bar(3, foo);
    

    or cast the ambiguous function name to a type from which the template argument can be deduced:

    bar(3, static_cast(foo));
    

    or wrap it in another function (or function object) to remove the ambiguity

    bar(3, [](int x){return foo(x);});
    

提交回复
热议问题