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

后端 未结 3 1835
遇见更好的自我
遇见更好的自我 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条回答
  • 2021-01-22 18:16

    The first case compiles well in MSVC10 with both std and boost (because MSVC10 doesn't support variadic templates, so std::bind is implemented similarly to boost::bind).

    The second case wouldn't compile, because bind can resolve overloads having different arity, but can't resolve overloads that differ by argument types only.

    0 讨论(0)
  • 2021-01-22 18:17

    It is somewhat documented as part of the "Interfaces" -> "Synopsis" part, where the overloads are listed. As you can see there, Boost does not use variadic templates, hence when given those overload, explicitly these:

    template<class R> unspecified-2 bind(R (*f) ());
    template<class F, class A1> unspecified-3-1 bind(F f, A1 a1);
    template<class R, class B1, class A1> unspecified-4 bind(R (*f) (B1), A1 a1);
    

    the compiler prefers the overloaded versions with an empty argument list over others as it is a better match. I don't think this was intentional, though.

    0 讨论(0)
  • 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.

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