CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 10 of 10

Thread: boost::bind

  1. #1
    John E is offline Elite Member Power Poster
    Join Date
    Apr 2001
    Location
    Manchester, England
    Posts
    4,835

    boost::bind

    Can anyone explain (simply!) what boost::bind does? From various things I've read it seems to be some way of simplifying operations on a vector (array). But whenever I've seen it used it's almost always used for handling sigc connections. So is it simply a way of adding things to an array so that they'll automatically get processed each time the array gets processed?

    Sorry... but I've yet to see a simple explanation for it...
    "A problem well stated is a problem half solved.” - Charles F. Kettering

  2. #2
    2kaud's Avatar
    2kaud is offline Super Moderator Power Poster
    Join Date
    Dec 2012
    Location
    England
    Posts
    7,822

    Re: boost::bind

    Isn't boost::bind the origins of std::bind which came with C++11?
    All advice is offered in good faith only. All my code is tested (unless stated explicitly otherwise) with the latest version of Microsoft Visual Studio (using the supported features of the latest standard) and is offered as examples only - not as production quality. I cannot offer advice regarding any other c/c++ compiler/IDE or incompatibilities with VS. You are ultimately responsible for the effects of your programs and the integrity of the machines they run on. Anything I post, code snippets, advice, etc is licensed as Public Domain https://creativecommons.org/publicdomain/zero/1.0/ and can be used without reference or acknowledgement. Also note that I only provide advice and guidance via the forums - and not via private messages!

    C++23 Compiler: Microsoft VS2022 (17.6.5)

  3. #3
    John E is offline Elite Member Power Poster
    Join Date
    Apr 2001
    Location
    Manchester, England
    Posts
    4,835

    Re: boost::bind

    You might well be right 2kaud - tho' that still doesn't make it any easier to understand...

    Here's what Google says when I ask it what std::bind does :-

    std::bind is a Standard Function Objects that acts as a Functional Adaptor i.e. it takes a function as input and returns
    a new function Object as an output with with one or more of the arguments of passed function bound or rearrange.
    and that's one of the better descriptions!
    "A problem well stated is a problem half solved.” - Charles F. Kettering

  4. #4
    2kaud's Avatar
    2kaud is offline Super Moderator Power Poster
    Join Date
    Dec 2012
    Location
    England
    Posts
    7,822

    Re: boost::bind

    Consider as a 'simple' example:

    Code:
    using std:: placeholders::_1;
    
    std::vector<int>v {1,2,3,4,5,6,7,8,9};
    
    const size_t val=std::count_if(v.begin(), v.end(),
        std::bind(std::logical_and<bool>(),
            std::bind(std::greater<int>(),_1,2),
                std::bind(std::less_equal<int>(),_1,6)));
    See https://wandbox.org/permlink/D7XjbyM0i2nslhRU

    is really the same as:

    Code:
    const size_t val=std::count_if(v.begin(), v.end(), [](auto x) {return x > 2 && x <= 6;});
    I never did much with std::bind (or std::bind1st or std::bind2nd which came previously) - when I needed a predicate prior to C++11 I just used a functor (class with operator() defined).

    PS Why do you need bind?
    All advice is offered in good faith only. All my code is tested (unless stated explicitly otherwise) with the latest version of Microsoft Visual Studio (using the supported features of the latest standard) and is offered as examples only - not as production quality. I cannot offer advice regarding any other c/c++ compiler/IDE or incompatibilities with VS. You are ultimately responsible for the effects of your programs and the integrity of the machines they run on. Anything I post, code snippets, advice, etc is licensed as Public Domain https://creativecommons.org/publicdomain/zero/1.0/ and can be used without reference or acknowledgement. Also note that I only provide advice and guidance via the forums - and not via private messages!

    C++23 Compiler: Microsoft VS2022 (17.6.5)

  5. #5
    Join Date
    Feb 2017
    Posts
    677

    Re: boost::bind

    I've never got into using bind because you regularly see articles like this,

    https://abseil.io/tips/108

    This article suggest using either lambdas or absl::bind_front instead, the latter probably written by the article author.

    But I note that there is an std::bind_front also,

    https://en.cppreference.com/w/cpp/ut...nal/bind_front

    and there it is stated that it is intended as a replacement for std::bind.

  6. #6
    2kaud's Avatar
    2kaud is offline Super Moderator Power Poster
    Join Date
    Dec 2012
    Location
    England
    Posts
    7,822

    Re: boost::bind

    But I note that there is an std::bind_front also,
    For C++20. Currently implemented in GCC 9 and VS2019 16.5 - but not yet Clang.

    For info about the proposal, see http://www.open-std.org/jtc1/sc22/wg...8/p0356r5.html
    All advice is offered in good faith only. All my code is tested (unless stated explicitly otherwise) with the latest version of Microsoft Visual Studio (using the supported features of the latest standard) and is offered as examples only - not as production quality. I cannot offer advice regarding any other c/c++ compiler/IDE or incompatibilities with VS. You are ultimately responsible for the effects of your programs and the integrity of the machines they run on. Anything I post, code snippets, advice, etc is licensed as Public Domain https://creativecommons.org/publicdomain/zero/1.0/ and can be used without reference or acknowledgement. Also note that I only provide advice and guidance via the forums - and not via private messages!

    C++23 Compiler: Microsoft VS2022 (17.6.5)

  7. #7
    John E is offline Elite Member Power Poster
    Join Date
    Apr 2001
    Location
    Manchester, England
    Posts
    4,835

    Re: boost::bind

    Quote Originally Posted by 2kaud View Post
    Consider as a 'simple' example:
    Haha - that's exactly what puzzles me... bind doesn't seem to simplify things at all - it seems to make everything a lot more complicated!

    Quote Originally Posted by 2kaud View Post
    PS Why do you need bind?
    Many years ago I helped develop a DAW product called Harrison Mixbus though I don't have much involvement these days. However, they just asked me to get involved again so I've been taking a look at the code - which is full of boost::bind now...
    "A problem well stated is a problem half solved.” - Charles F. Kettering

  8. #8
    John E is offline Elite Member Power Poster
    Join Date
    Apr 2001
    Location
    Manchester, England
    Posts
    4,835

    Re: boost::bind

    Quote Originally Posted by wolle View Post
    I've never got into using bind because you regularly see articles like this,

    https://abseil.io/tips/108
    That's a good article wolle - thanks for finding it.

    I must admit though... I still don't understand what bind does (that's useful...)
    "A problem well stated is a problem half solved.” - Charles F. Kettering

  9. #9
    2kaud's Avatar
    2kaud is offline Super Moderator Power Poster
    Join Date
    Dec 2012
    Location
    England
    Posts
    7,822

    Re: boost::bind

    All advice is offered in good faith only. All my code is tested (unless stated explicitly otherwise) with the latest version of Microsoft Visual Studio (using the supported features of the latest standard) and is offered as examples only - not as production quality. I cannot offer advice regarding any other c/c++ compiler/IDE or incompatibilities with VS. You are ultimately responsible for the effects of your programs and the integrity of the machines they run on. Anything I post, code snippets, advice, etc is licensed as Public Domain https://creativecommons.org/publicdomain/zero/1.0/ and can be used without reference or acknowledgement. Also note that I only provide advice and guidance via the forums - and not via private messages!

    C++23 Compiler: Microsoft VS2022 (17.6.5)

  10. #10
    Join Date
    Feb 2017
    Posts
    677

    Re: boost::bind

    Quote Originally Posted by John E View Post
    I must admit though... I still don't understand what bind does (that's useful...)
    Maybe it is a question of programming style. Traditionally in procedural programming, if you don't have all parameters immediately available to call a function you gather them by asking around for them and then you call the function. In object oriented programming and, I think, in functional programming you avoid to "pull data" in this way. Instead you "push requests" to get others to do things for you. In such designs it may feel more natural to pass around a function and allow others to partially evaluate it for you by binding the parameters they have naturally available.
    Last edited by wolle; October 13th, 2020 at 03:01 AM.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured