C++中swap函数的特化,使用XCode发现的问题,有关target membership
用XCode写C++的代码,代码如下:
#include<utility>
class Widget
{
public:
Widget();
Widget(Widget &);
Widget &operator=(Widget &);
void swap(Widget &);
private:
int x;
};
Widget::Widget()
{
x = 1;
}
Widget::Widget(Widget &rhs) :x(rhs.x)
{
}
Widget &Widget::operator=(Widget &rhs)
{
x = rhs.x;
return *this;
}
void Widget::swap(Widget &other)
{
using std::swap;
swap(x, other.x);
}
namespace std
{
template<>
void swap<Widget>(Widget &a, Widget &b)
{
a.swap(b);
}
}
int main()
{
Widget a;
Widget b;
std::swap(a, b);
return 0;
}
只是想试试swap的特化而已,结果一直编译不成功,提示no function template matches function template specialization 'swap'.
在VS里面能成功编译。
后来无意间把target membership里面的勾给去掉了,结果就编译成功了。
所以想知道为什么勾上了target membership之后就不能编译了。。。
平常只是用XCode简单的打打代码,不是很了解这些东西。
希望高人指教。