有没有办法将重载的类函数绑定到函数对象?

Is there a way to bind overloaded class functions to a function object?

本文关键字:绑定 函数 对象 类函数 重载 有没有      更新时间:2023-10-16

我是函数绑定概念的新手。我需要基于参数数量的类成员函数重载,我想绑定这些函数。我还有一个疑问,即带有变量参数的函数对象是否可行。

例:

class A{
void print(int i)
{
};
void print(int i,int j){
};
}; 
//inside the object of A can I create function object like this??
auto f=std::bind(&A::print, this, std::placeholders::_1,...);

在具有精确签名的上下文中,会自动选择适当的重载:

void (A::*p_i)(int) = &A::print;
void (A::*f_ii)(int, int) = &Demo::f;

在无法推断签名的上下文中:

//auto f_a = &A::print; // ambiguous - which one???

您可以使用强制转换显式选择:

auto f_a = static_cast<void (A::*)(int)>(&A::print);