如何将一种类型的unique_ptr复制到另一种类型
How copying a unique_ptr of one type to another works
参照以下代码
#include <iostream>
#include <memory>
using std::cout;
using std::endl;
using std::make_unique;
struct Base {};
struct Derived : public Base {};
int main() {
auto base_uptr = std::unique_ptr<Base>{make_unique<Derived>()};
return 0;
}
哪个构造函数被unique_ptr
调用?我看了看cppreference.com和我找到的构造函数(c++17之前)是(为了完成)
constexpr unique_ptr();
constexpr unique_ptr( nullptr_t );
explicit unique_ptr( pointer p );
unique_ptr( pointer p, /* see below */ d1 );
unique_ptr( pointer p, /* see below */ d2 );
unique_ptr( unique_ptr&& u );
template< class U, class E >
unique_ptr( unique_ptr<U, E>&& u );
它们似乎都不接受其他类型的指针。我错过了什么?调用哪个构造函数?
谢谢!
第六,模板构造函数。
template< class U, class E >
unique_ptr( unique_ptr<U, E>&& u );
要求" unique_ptr<U, E>::pointer
可隐式转换为pointer
"。换句话说,U*
需要隐式转换为unique_ptr
存储的指针类型。在您的情况下,它是,因为Derived*
隐式地转换为Base*
。
相关文章:
- ArduinoJson 6.15.2:JsonObject没有命名类型
- 防止主数据类型C++的隐式转换
- 大量序列中核苷酸类型的快速计数
- 如何从C++中的依赖类型中获得它所依赖的类型
- 有关插入适配器的错误。[错误]请求从 'back_insert_iterator<vector<>>' 类型转换为非标量类型
- 是否可以初始化不可复制类型的成员变量(或基类)
- 如何获取std::result_of函数的返回类型
- 从父命名空间重载类型
- 如果C++类在类方法中具有动态分配,但没有构造函数/析构函数或任何非静态成员,那么它仍然是POD类型吗
- 我想将一个对T类型的非常量左值引用绑定到一个T类型的临时值
- Openssl 1.1.1d无效使用不完整的类型"struct dsa_st"
- 访问者访问变体并返回不同类型时出错
- 为什么 std::unique 不调用 std::sort?
- 在VS2010-VS2015下编译时,如何使用decltype作为较大类型表达式的LHS
- 处理小于cpu数据总线的数据类型.(c++转换为机器代码)
- C++ 雷神库 - 使用资源加载器类时出现问题(不命名类型)
- 模板元程序查找相似的连续类型名称
- 是否可以从int转换为enum类类型
- 构造函数正在调用一个使用当前类类型的函数
- 我应该使用什么来代替void作为变体中的替代类型之一