绑定重载函数

Binding overloaded function

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

这段代码是开发并最初与boost 1.48一起使用的。自从更新到 boost 1.52 以来,它拒绝编译。

boost::signals2::signal<void (uint32_t)> foo;
boost::shared_ptr<boost::circular_buffer<uint32_t>> _foo_buffer = new boost::circular_buffer<uint32_t>(32);
_foo.connect(boost::bind(&boost::circular_buffer<uint32_t>::push_front, _foo_buffer, _1));

目标是将foo(42) 42放在圆形缓冲区的前面。

我的编译错误与一堆Candidate template ignored: couldn't infer template argument 'R'和类似的模板错误No matching function for call to 'bind'

怀疑问题是在我使用的增强版本之间,push_front的定义从

void push_front(const_reference item = value_type());

到三个定义

void push_front(param_value_type);
void push_front(rvalue_type);
void push_front();

这让我的编译器感到困惑。

我非常感谢有关连接表达式语法的一些帮助,该表达式将与新的 boost 库一起使用。谢谢

假设您有两个函数

void foo( int ) {}
void foo( double* ) {}

那么你不能只写&foo因为那样会模棱两可。

但是您可以使用static_cast来建立一个可能只有一个候选项的上下文,因此:

static_cast<void(*)(int)>( &foo )

它为您提供第一个的地址,第二个地址也是如此。


也就是说,看起来您可以通过使用 C++ lambda 函数而不是 bind 来简化事情(对于 C++03 编译器,或者定义您自己的函子)。