boost::bind, std::bind and overloaded functions

后端 未结 3 1839
遇见更好的自我
遇见更好的自我 2021-01-22 17:36

I noticed that boost::bind, unlike std::bind, can work with overloaded functions when one of these functions doesn\'t have any parameters. Am I right? Is this documented?

<
3条回答
  •  慢半拍i
    慢半拍i (楼主)
    2021-01-22 18:20

    I would assume it is just an unintended artifact of the implementation details, I don't think Boost provides any guarantees about automatically resolving the correct overload.

    std::bind is a C++11 feature implemented with variadic templates.

    boost::bind was implemented for C++03, meaning that it relies on a large number of overloaded function templates.

    The two are quite different in their implementation details, and so, I would assume any difference between their behavior is a consequence of that, rather than an intentional and specified difference.

    The Boost documentation only states this: "An attempt to bind an overloaded function usually results in an error, as there is no way to tell which overload was meant to be bound."

    In my book, this means that Boost docs tell you that it is "undefined behavior" whether or not this will work (compile) or even select the correct overload.

    And as far as I know, you should always use an explicit cast (static_cast) to fix the signature of the overload that you wish to select. This is true for both boost::bind and std::bind, and in that sense, both implementations agree.

提交回复
热议问题