绑定重载函数
Binding overloaded function
这段代码是开发并最初与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 编译器,或者定义您自己的函子)。
相关文章:
- 为什么使用SFINAE而不是函数重载
- 为什么我不能在 C++ 中的特定函数重载中调用同一函数的任何其他重载?
- c++:可变模板和函数重载
- 在缺少函数重载时抛出异常,并带有 std::variant 而不是编译时错误
- 解决模板成员函数重载
- 为什么不允许成员函数和非成员函数之间的函数重载?
- 推断模板化函数中的函数重载
- C++复制函数重载导致"must be a nonstatic member function"错误
- 为什么 std::sort 找不到合适的(静态成员)函数重载?
- 可变参数泛型 lambda 和函数重载
- C++中的函数重载和继承
- 当有右值构造函数可用时,为什么从右值调用类引用构造函数重载?
- C/C++ 可变参数宏函数重载
- 将基类的成员函数重载到其他派生类C++
- C++ 函数重载匹配
- C++函数重载,具体步骤是什么
- C++:使用 param pack 显式调用函数重载
- 隐式生成的函数重载用于右值参数?
- 使用函数重载输入运算符
- 运算符重载函数上的函数重载