关于网友提出的“ 一个在std空间内特化的问题。”问题疑问,本网通过在网上对“ 一个在std空间内特化的问题。”有关的相关答案进行了整理,供用户进行参考,详细问题解答如下:
问题: 一个在std空间内特化的问题。描述:
c++
今天写了段代码Widget.h内容:
#include
namespace WidgetStuff
{
using std::vector;
class WidgetImpl
{
private:
int a{ 0 }, b{ 0 }, c{ 0 };
vector
public:
WidgetImpl(int num1 = 0, int num2 = 0, int num3 = 0, vector
};
class Widget
{
private:
WidgetImpl* pImpl;
public:
Widget() : pImpl(new WidgetImpl()) {}
Widget(WidgetImpl &par) : pImpl(new WidgetImpl()) { WidgetImpl *ptr(&par); pImpl = ptr; }
Widget(const Widget& rhs);
Widget& operator=(const Widget& rhs);
void swap(Widget& other);
~Widget() { if (pImpl) delete pImpl; }
};
}
Widget.cpp内容:
#include "Widget.h"
namespace WidgetStuff
{
Widget::Widget(const Widget& rhs) : pImpl(new WidgetImpl(*rhs.pImpl)) {}
Widget& Widget::operator=(const Widget& rhs)
{
WidgetImpl* pro = pImpl;
*pImpl = *(rhs.pImpl);
delete pro;
return *this;
}
void Widget::swap(Widget& other)
{
using std::swap;
swap(pImpl, other.pImpl);
}
}
main.cpp内容:
#include "Widget.h"
#include
using std::system;
namespace std
{
template<>
void swap
{
a.swap(b);
}
}
int main()
{
system("pause");
return 0;
}
这样编译报错:
1>d:\cc\effective\effective\main.cpp(9): error C2065: “Widget”: 未声明的标识符
1>d:\cc\effective\effective\main.cpp(9): error C2065: “a”: 未声明的标识符
1>d:\cc\effective\effective\main.cpp(9): error C2065: “b”: 未声明的标识符
1>d:\cc\effective\effective\main.cpp(9): error C2182: “swap”: 非法使用“void”类型
1>d:\cc\effective\effective\main.cpp(9): error C2365: “std::swap”: 重定义;以前的定义是“函数”
1>d:\cc\effective\effective\main.cpp(12): error C2399: 此版本中不支持变量模板
为什么特化那段是错的?不解?
解决方案1:
template<>
void swap
a.swap (b);
}