问题上的boost ::互换 [英] Question on boost::swap
问题描述
在问题情侣的boost ::交换
。请参考code以下基本上是从升压/ swap.hpp
剪切粘贴。我指的是库版本1.43.0。
命名空间boost_swap_impl
{
模板<类T>
无效swap_impl(T&安培;离开后,T&安培;右)
{
使用命名空间std; //使用std ::互换,如果参数依赖查找失败
互换(左,右);
} 模板< T类,的std ::为size_t N'GT;
无效swap_impl(T(安培;左)[N],T(安培;右)[N])
{
对于(的std ::为size_t我= 0; I< N ++ I)
{
:: boost_swap_impl :: swap_impl(左[I],右[I]);
}
}
}名字空间boost
{
模板<组别为T1,T2类GT&;
无效掉期(T1&安培;左,T2和放大器;右)
{
:: boost_swap_impl :: swap_impl(左,右);
}
}
- 为什么声明
的boost ::交换
为模板< typename的T1,typename的T2>
在休息的时候在code的它是所有处理相同类型? - 如果我定义自己的全局函数
无效掉期(T&安培,T&放大器;)
我看到它是全局函数,它会从称为swap_impl(T&安培;离开后,T&安培;右)
。这难道不是因为swap_impl
冲突,因此错误条件也使用空间std
已掉界定?
- 这使得它比
的std ::交换专业化程度较低
,所以你不要超载歧义错误当两个的std ::互换
和的boost ::交换
在范围(的std ::互换
将采取precedence) - 无,无模板总是有重载决议时超过模板precedence,所以一个命名空间范围的非模板
交换
将采取precedence超过两个的boost ::交换
和的std ::互换
(将在命名空间范围的模板交换
超载对于UDT&ndash的;认为部分专业化,但不是真的..)。注意,不像的std ::互换
,的boost ::交换
被明确写入利用的 ADL 。
下面就是在C ++标准03对有两个点和ndash的说; [over.match.best](§13.3.3/ 1):
定义ICS 我(
˚F
)如下:
- 如果
˚F
是一个静态成员函数,ICS 1 (˚F
)是定义为使得ICS 1 (˚F
)既不是比ICS更好,也不更坏的 1 (摹
)的任何函数,并且,对称,ICS 1 ( )既不是比ICS好也不变坏 1 ( ˚F
);否则,
- 让ICS 我(
˚F
)表示该转换的 I 的-th参数的隐式转换序列列表中的类型的 I 的可行函数的参数-th˚F
。 13.3.3.1定义了隐式转换序列和13.3.3.2定义是什么意思一个隐式转换序列比另一种更好的转换序列或更糟的转换序列。
由于这些定义,一个可行的函数
F1
被定义为一个的更好的功能比另一个可行函数F2
如果所有参数的 I 的,ICS 我(F1
)不是一个糟糕的转换序列比ICS 我(F2
),然后
- 对于一些参数的Ĵ的,ICS Ĵ(
F1
)比ICS更好的转换序列 Ĵ(F2
),或者,如果不说,
F1
是一个非模板函数和F2
是一个函数模板专业化,或者,如果不说,
F1
和F2
是函数模板特,以及函数模板F1
比模板F2
根据14.5.5.2描述的部分排序规则,或更专业化的,如果不是,
- 的上下文是由用户定义的转换初始化(见8.5,13.3.1.5和13.3.1.6)和
F1
的返回类型的标准转换序列为目标类型(即实体的类型被初始化)比从F2
的目的地类型的返回类型的标准转换序列更好的转换序列。
块引用>Couple of questions on
boost::swap
. Please refer to the code below which is basically a cut-paste fromboost/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::swap
declared 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_impl
also usesnamespace std
which has swap defined?
解决方案
- This makes it less specialized than
std::swap
so you don't get overload ambiguity errors when bothstd::swap
andboost::swap
are in scope (std::swap
will take precedence).- No, non-templates always have precedence over templates during overload resolution, so a namespace-scoped non-template
swap
will take precedence over bothboost::swap
andstd::swap
(as will a namespace-scoped templateswap
overloaded for a UDT – think partially-specialized, but not really..). Note that unlikestd::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 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
F1
is defined to be a better function than another viable functionF2
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 andF2
is a function template specialization, or, if not that,F1
andF2
are function template specializations, and the function template forF1
is more specialized than the template forF2
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 ofF2
to the destination type.
这篇关于问题上的boost ::互换的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!