Question on boost::swap

前端 未结 1 889
名媛妹妹
名媛妹妹 2021-01-01 22:23

Couple of questions on boost::swap. Please refer to the code below which is basically a cut-paste from boost/swap.hpp. I am referring to library ve

相关标签:
1条回答
  • 2021-01-01 23:08
    1. This makes it less specialized than std::swap so you don't get overload ambiguity errors when both std::swap and boost::swap are in scope (std::swap will take precedence).
    2. No, non-templates always have precedence over templates during overload resolution, so a namespace-scoped non-template swap will take precedence over both boost::swap and std::swap (as will a namespace-scoped template swap overloaded for a UDT – think partially-specialized, but not really..). Note that unlike std::swap, boost::swap is written explicitly to take advantage of ADL.

    Here's what the C++03 standard has to say regarding both points – [over.match.best] (§13.3.3/1):

    Define ICSi(F) as follows:

    • if F is a static member function, ICS1(F) is defined such that ICS1(F) is neither better nor worse than ICS1(G) for any function G, and, symmetrically, ICS1(G) is neither better nor worse than ICS1(F); otherwise,
    • let ICSi(F) denote the implicit conversion sequence that converts the i-th argument in the list to the type of the i-th parameter of viable function F. 13.3.3.1 defines the implicit conversion sequences and 13.3.3.2 defines what it means for one implicit conversion sequence to be a better conversion sequence or worse conversion sequence than another.

    Given these definitions, a viable function F1 is defined to be a better function than another viable function F2 if for all arguments i, ICSi(F1) is not a worse conversion sequence than ICSi(F2), and then

    • for some argument j, ICSj(F1) is a better conversion sequence than ICSj(F2), or, if not that,
    • F1 is a non-template function and F2 is a function template specialization, or, if not that,
    • F1 and F2 are function template specializations, and the function template for F1 is more specialized than the template for F2 according to the partial ordering rules described in 14.5.5.2, or, if not that,
    • the context is an initialization by user-defined conversion (see 8.5, 13.3.1.5, and 13.3.1.6) and the standard conversion sequence from the return type of F1 to the destination type (i.e., the type of the entity being initialized) is a better conversion sequence than the standard conversion sequence from the return type of F2 to the destination type.
    0 讨论(0)
提交回复
热议问题