std::unique_ptr 和模板:为什么这段代码无法编译?
std::unique_ptr and templates: Why won't this code compile?
我正在制作一款游戏的模板化组件系统。它将元素保存在vector中,并有两个部分专门化:一个用于POD类型,另一个使用std::unique_ptr。std::unique_ptr的vector的模板化特化不能编译,但在非模板化代码中使用std::unique_ptr的vector可以正常工作。我一直在寻找这个问题的解决方案,但不幸的是,我对c++ 11/c++ 14的了解不够完善;我做错了什么?
最小示例(剥离到问题区域):
#include <vector>
#include <memory>
template<typename T>
class component
{
public:
class handle
{
friend class component;
protected:
std::size_t inner;
};
component<T>::handle add(const T& t)
{
items.push_back(t);
handle h;
h.inner = items.size() - 1;
return h;
}
protected:
std::vector<T> items;
};
template<typename T>
class component<std::unique_ptr<T>>
{
public:
class handle
{
friend class component;
protected:
std::size_t inner;
};
component<std::unique_ptr<T>>::handle add(const std::unique_ptr<T>& t)
{
items.push_back(std::move(t));
handle h;
h.inner = items.size() - 1;
return h;
}
protected:
std::vector<std::unique_ptr<T>> items;
};
下面是测试代码
int main()
{
// This works fine.
component<int> pod_component;
pod_component.add(5);
// This works, too!
std::vector<std::unique_ptr<int>> pointer_vector;
pointer_vector.push_back(std::make_unique<int>(5));
component<std::unique_ptr<int>> pointer_component;
// Why doesn't this compile?
pointer_component.add(std::make_unique<int>(5));
return 0;
}
下面是gcc (4.9.3 Gentoo)的错误输出:
error: use of deleted function ‘std::unique_ptr<_Tp, _Dp>::unique_ptr(const std::unique_ptr<_Tp, _Dp>&) [with _Tp = int; _Dp = std::default_delete<int>]’
{ ::new((void *)__p) _Up(std::forward<_Args>(__args)...); }
我希望有比我更精通c++的人可以帮助我和未来的读者理解这个棘手的问题。
问题就在这里:
component<std::unique_ptr<T>>::handle add(const std::unique_ptr<T>& t)
{ // |
items.push_back(std::move(t));// <------+
// ...
std::move
应用于const
左值返回const
右值引用。因此,它受push_back(const value_type&)
重载而不是push_back(value_type&&)
重载的约束,这意味着它试图复制作为参数传递的unique_ptr
。
正确的声明如下:
component<std::unique_ptr<T>>::handle add(std::unique_ptr<T>&& t)
// ~^^~
相关文章:
- 如何修复sfml c++代码编译错误
- 尝试用java代码编译和运行c++代码
- 此代码编译良好,但文件未创建?请指出错误
- 为什么我的 BaseClass:Method 代码编译(带有单冒号)?
- 代码编译没有任何输出,入门程序
- 通过Python Distutils(用于Python C扩展)使用可重定位的设备代码编译CUDA代码
- 代码编译但不起作用!cmd窗口只是理想和理想,但什么也没发生
- 无法使用 g++ 使用C++代码编译 C 库
- 如何在Ubuntu中使用Visual Studio代码编译C++代码
- 推力+提升代码编译错误
- C++代码编译,但在 Zorin OS 上运行时显示错误
- 如何在使用模板时将 CPP 代码编译到库文件中
- 是否可以将Visual Studio 2017将C 代码编译到EXE以外的文件类型中
- 使用 Visual Studio for C++ 代码编译错误
- 使用 Android Studio 使用本机代码编译 apk 时,如何在链接处删除 libgnustl_static.
- 使用 Emscripten 将 OpenCV 代码编译C++ Javascript
- 将C 代码编译到独立应用程序.App
- 为什么此代码编译 (C++11) 而没有类型不匹配错误
- 重用编译器前端的结果,以加快多个配置/平台的C++代码编译
- 如何将C++11代码编译为网络汇编