boost :: bind()绑定额外的参数?

冲浪码

boost :: bind()绑定是否会绑定额外的参数,因为似乎将没有参数的绑定函数传递给了一个期望参数加倍的函数就可以了?如果我要明确写出绑定函数,那应该是什么?

struct MyClass
{
    void f() 
    {
        std::cout << "f()" << std::endl;
    }
};

void bar( const boost::function<void(const double&)>& f )
{
    f( 1.0 );
}

int main()
{
    MyClass c;

    // why this compiles
    bar( boost::bind( &MyClass::f, &c ) );

    // what should I write if I want to create the binded function explicitly before pass into bar?
    // boost::function<void(const double&)> f = boost::bind( ... boost::bind( &MyClass::f, &c ), ?? )
    bar( f );

}
看到

这是设计使然1.0,调用bind-expression时传递的未绑定参数(例如)将被忽略。

boost::function<void(const double&)> f = boost::bind(&MyClass::f, &c);
bar(f);

对于绑定表达式的显式分配会很好。

更新评论:

请记住,有两个准则:

  • function<...> 有固定的签名
  • bind表达式没有固定的签名。的整个目的bind更改签名。这包括例如

    • 添加状态以填充从签名中删除的形式参数或
    • 添加参数,通过不将其绑定到目标可调用项来忽略
    • 通过使用隐式转换来更改参数/返回类型
    • 甚至可以更改参数绑定到目标可调用对象的顺序,而签名在技术上可以保持不变。

因此,虽然您不能func<...>为彼此分配不同的类型,但始终可以bind将一个签名分配给另一个。

这是一个更完整的演示,它显示了您可以使用function进行操作的局限性bind以及原因(操作方式):Live On Coliru

#include <boost/function.hpp>
#include <boost/bind.hpp>
#include <iostream>
#include <cassert>

int foo0()            { return 0; }
int foo1(int)         { return 1; }
int foo2(int,int)     { return 2; }
int foo3(int,int,int) { return 3; }

int main()
{
    boost::function<int()>            func0;
    boost::function<int(int)>         func1;
    boost::function<int(int,int)>     func2;
    boost::function<int(int,int,int)> func3;

    // "straight" assignment ok:
    // -------------------------
    func0 = foo0;                          assert (0 == func0());
    func1 = foo1;                          assert (1 == func1(-1));
    func2 = foo2;                          assert (2 == func2(-1,-1));
    func3 = foo3;                          assert (3 == func3(-1,-1,-1));

    // "mixed" assignment not ok:
    // --------------------------
    // func0 = foo1;                       // compile error
    // func3 = foo2;                       // compile error
    // func1 = func2;                      // compile error, just the same
    // func2 = func1;                      // compile error, just the same

    // SOLUTION: you can always rebind:
    // --------------------------------
    func0 = boost::bind(foo3, 1, 2, 3);    assert (func0() == 3);
    func3 = boost::bind(foo1, _3);         assert (func3(-1,-1,-1) == 1);
    func3 = boost::bind(foo2, _3, _2);     assert (func3(-1,-1,-1) == 2);
    // same arity, reversed arguments:
    func3 = boost::bind(foo3, _3, _2, _1); assert (func3(-1,-1,-1) == 3);

    // can't bind more than number of formal parameters in signature:
    // --------------------------------------------------------------
    // func3 = boost::bind(foo1, _4);      // in fact, the bind is fine, but assigning to `func3` fails
}

所有断言都通过了。当您取消注释未编译的行时,可以尝试使用编译器说的内容。

干杯

本文收集自互联网,转载请注明来源。

如有侵权,请联系[email protected] 删除。

编辑于
0

我来说两句

0条评论
登录后参与评论

相关文章

来自分类Dev

访问带有额外参数的boost变体

来自分类Dev

如何将boost ::: bind绑定到boost :: math :: pdf?

来自分类Dev

boost :: bind与模板函子绑定

来自分类Dev

Boost Tokenizer:额外的空间?

来自分类Dev

boost :: bind为void指针参数

来自分类Dev

使用 boost::bind 结果作为参数

来自分类Dev

使用boost :: function和boost :: bind

来自分类Dev

使用boost :: function和boost :: bind

来自分类Dev

Boost Asio - boost::bind 导致程序崩溃

来自分类Dev

boost asio绑定端点

来自分类Dev

boost asio绑定端点

来自分类Dev

使用boost :: bind与__fastcall

来自分类Dev

使用 boost 的模板实例化:传递额外的参数

来自分类Dev

如何使用boost :: bind将静态成员函数绑定到boost :: function

来自分类Dev

如何使用boost :: bind将静态成员函数绑定到boost :: function

来自分类Dev

boost :: bind线程用于带参数的函数指针

来自分类Dev

使用boost :: bind但允许传递任何其他参数

来自分类Dev

ASIO处理程序参数和boost :: bind,编译时错误

来自分类Dev

C ++ / Boost:将成员函数作为参数传递给boost :: bind

来自分类Dev

澄清使用`boost :: bind`和`this`

来自分类Dev

澄清使用`boost :: bind`和`this`

来自分类Dev

从boost :: bind_t自动转换为boost :: function

来自分类Dev

绑定端点时的boost :: asio绑定异常

来自分类Dev

以C ++ 11方式重写Boost风格的代码(Boost.Bind,Boost.Function)

来自分类Dev

boost :: phoenix :: bind和boost :: phoenix :: actors在语义动作中对boost :: spirit :: qi的问题

来自分类Dev

std :: bind和boost :: bind之间的差异

来自分类Dev

std :: bind和boost :: bind技巧

来自分类Dev

std :: bind和boost :: bind技巧

来自分类Dev

Boost Asio绑定读取处理程序

Related 相关文章

热门标签

归档