函数指针模糊度与模板化参数

mik*_*bal 1 c++ templates function-pointers

我正在尝试将重载函数指针作为参数传递给模板函数.

float Function1(float par1)
{
 return 0;  
}

float Function1(float par1, float par2)
{
 return 0;  
}

template<typename R, typename A1>
void Bind(R(*func)(A1))
{
   std::cout << "Correct one called\n";
}

template<typename R, typename A1, typename A2>
void Bind(R(*func)(A1, A2))
{
   std::cout << "False one called\n";
}

int main()
{
 Bind<float, float>(&Function1);
}
Run Code Online (Sandbox Code Playgroud)

即使我用2浮点参数明确调用函数,编译器似乎无法解析正确的调用.编译器显示"模糊函数调用"错误.

我在这里创建了一个小样本:http: //liveworkspace.org/code/4kVlUY$195

这个错误的原因是什么?谢谢.

mfo*_*ini 6

当你试图占据Function1地址时会出现歧义.编译器看到2个重载,它不知道你指的是哪一个.您需要明确指出您想要的那个:

Bind(
    static_cast<float(*)(float, float)>(&Function1)
);
Run Code Online (Sandbox Code Playgroud)

您在调用时明确指出了模板参数Bind,但为时已晚,在该点之前发现了歧义.