这里我写一个代码片段看看是哪个swap
会被调用,但结果都不是。什么也没有输出。
#include<iostream>
class Test {};
void swap(const Test&lhs,const Test&rhs)
{
std::cout << "1";
}
namespace std
{
template<>
void swap(const Test&lhs, const Test&rhs)
{
std::cout << "2";
}
/* If I remove the const specifier,then this will be called,but still not the one in global namespace,why?
template<>
void swap(Test&lhs, Test&rhs)
{
std::cout << "2";
}
*/
}
using namespace std;
int main()
{
Test a, b;
swap(a, b);//Nothing outputed
return 0;
}
Which swap
叫做?而在另一种情况下,为什么专门swap
没有const
称为说明符,而不是::swap
?
std::swap()
是这样的[ref http://en.cppreference.com/w/cpp/algorithm/swap]
template< class T >
void swap( T& a, T& b );
这是比你更好的匹配
void swap(const Test& lhs, const Test& rhs);
for
swap(a, b);
where a
and b
are 非常量. So std::swap()
被调用,它什么也不输出。
注意std::swap()
参与重载决策,因为using namespace std;
.
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)