Question on boost::swap
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 version 1.43.0.
namespace boost_swap_impl
{
template<class T>
void swap_impl(T& left, T& right)
{
using namespace std;//use std::swap if argument dependent lookup fails
swap(left,right);
}
template<class T, std::size_t N>
void swap_impl(T (& left)[N], T (& right)[N])
{
for (std::size_t i = 0; i < N; ++i)
{
::boost_swap_impl::swap_impl(left[i], right[i]);
}
}
}
namespace boost
{
template<class T1, class T2>
void swap(T1& left, T2& right)
{
::boost_swap_impl::swap_impl(left, right);
}
}
- Why is
boost::swapdeclared astemplate <typename T1, typename T2>when in the rest of the code it is all dealing with the same type? - If I define my own global function
void swap(T&, T&)I see that开发者_运维知识库 it is the global function that gets called fromswap_impl(T& left, T& right). Is this not a conflict and hence an error condition sinceswap_implalso usesnamespace stdwhich has swap defined?
- This makes it less specialized than
std::swapso you don't get overload ambiguity errors when bothstd::swapandboost::swapare in scope (std::swapwill take precedence). - No, non-templates always have precedence over templates during overload resolution, so a namespace-scoped non-template
swapwill take precedence over bothboost::swapandstd::swap(as will a namespace-scoped templateswapoverloaded for a UDT – think partially-specialized, but not really..). Note that unlikestd::swap,boost::swapis 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
Fis a static member function, ICS1(F) is defined such that ICS1(F) is neither better nor worse than ICS1(G) for any functionG, 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 functionF. 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
F1is defined to be a better function than another viable functionF2if 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,F1is a non-template function andF2is a function template specialization, or, if not that,F1andF2are function template specializations, and the function template forF1is more specialized than the template forF2according 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
F1to 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 ofF2to the destination type.
加载中,请稍侯......
精彩评论