What is the return type of boost::bind?

后端 未结 1 1915
名媛妹妹
名媛妹妹 2020-12-03 20:55

I want to save the \"binder\" of a function to a variable, to use it repetitively in the following code by exploiting its operator overloading facilities. Here is the code t

相关标签:
1条回答
  • 2020-12-03 21:09

    The short answer is: you don't need to know (implementation defined). It is a bind expression (std::tr1::is_bind_expression<T>::value yields true for the actual type).

    Look at

    1. std::tr1::function<>
    2. BOOST_AUTO()
    3. c++0x 'auto' keywords (Type Inference)
      • it's close cousing decltype() can help you move further

    1.

    std::tr1::function<int> f; // can be assigned from a function pointer, a bind_expression, a function object etc
    
    int realfunc();
    int realfunc2(int a);
    
    f = &realfunc;
    int dummy;
    f = tr1::bind(&realfunc2, dummy);
    

    2.

    BOOST_AUTO() aims to support the semantics of c++0x auto without compiler c++0x support:

    BOOST_AUTO(f,boost::bind(&T::some_complicated_method, _3, _2, "woah", _2));
    

    3.

    Essentially the same but with compiler support:

    template <class T> struct DoWork { /* ... */ };
    
    auto f = boost::bind(&T::some_complicated_method, _3, _2, "woah", _2));
    
    DoWork<decltype(T)> work_on_it(f); // of course, using a factory would be _fine_
    

    Note that auto has probably been invented for this kind of situation: the actual type is a 'you don't want to know' and may vary across compilers/platforms/libraries

    0 讨论(0)
提交回复
热议问题