是否可以将重载的类函数绑定到函数对象?

问题描述 投票:0回答:1

我是函数绑定概念的新手。我需要根据参数数量重载类成员函数,并且我想绑定这些函数。我还不确定是否可以使用带有可变参数的函数对象。

示例:

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,...);
c++ function overloading bind
1个回答
0
投票
在具有正确签名的上下文中,将自动选择适当的重载:

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);

© www.soinside.com 2019 - 2024. All rights reserved.