草庐IT

together_unique

全部标签

c++ - 在 C++11 中将对象的所有权从一个 unique_ptr 转移到另一个 unique_ptr?

在C++11中,我们可以使用std::move()将一个对象的所有权转移到另一个unique_ptr。所有权转移后,让出所有权的智能指针变为null,get()返回nullptr.std::unique_ptrp1(newint(42));std::unique_ptrp2=std::move(p1);//Transferownership在将所有权转移到另一个unique_ptr时,这在哪些情况下有用? 最佳答案 以下情况涉及从一个unique_ptr转移所有权另一个:从函数返回,并作为参数传递给构造函数等函数。假设你有一些多态类

c++ - 在 C++11 中将对象的所有权从一个 unique_ptr 转移到另一个 unique_ptr?

在C++11中,我们可以使用std::move()将一个对象的所有权转移到另一个unique_ptr。所有权转移后,让出所有权的智能指针变为null,get()返回nullptr.std::unique_ptrp1(newint(42));std::unique_ptrp2=std::move(p1);//Transferownership在将所有权转移到另一个unique_ptr时,这在哪些情况下有用? 最佳答案 以下情况涉及从一个unique_ptr转移所有权另一个:从函数返回,并作为参数传递给构造函数等函数。假设你有一些多态类

c++ - 如何在构造函数中初始化 std::unique_ptr?

A.hpp:classA{private:std::unique_ptrfile;public:A(std::stringfilename);};A.cpp:A::A(std::stringfilename){this->file(newstd::ifstream(filename.c_str()));}我得到的错误被抛出:A.cpp:7:43:error:nomatchforcallto‘(std::unique_ptr>)(std::ifstream*)’有没有人知道为什么会发生这种情况?我尝试了许多不同的方法来让它工作,但都无济于事。 最佳答案

c++ - 如何在构造函数中初始化 std::unique_ptr?

A.hpp:classA{private:std::unique_ptrfile;public:A(std::stringfilename);};A.cpp:A::A(std::stringfilename){this->file(newstd::ifstream(filename.c_str()));}我得到的错误被抛出:A.cpp:7:43:error:nomatchforcallto‘(std::unique_ptr>)(std::ifstream*)’有没有人知道为什么会发生这种情况?我尝试了许多不同的方法来让它工作,但都无济于事。 最佳答案

c++ - 持有派生类引用的基类的 std::unique_ptr 在 gcc 编译器中不显示警告,而裸指针显示它。为什么?

我有一个基类和派生类的层次结构。基类有一个被派生类覆盖的虚函数。classBase{public:~Base();virtualvoidother_functionality()=0;};classDerived:publicBase{public:~Derived();voidother_functionality(){//somecode};};现在如果我这样做:intmain(){Base*P=newDerived();deletep;return0;}报错:删除具有非虚析构函数的多态类类型的对象。但是使用unique_ptr它会毫无警告地通过。intmain(){std::un

c++ - 持有派生类引用的基类的 std::unique_ptr 在 gcc 编译器中不显示警告,而裸指针显示它。为什么?

我有一个基类和派生类的层次结构。基类有一个被派生类覆盖的虚函数。classBase{public:~Base();virtualvoidother_functionality()=0;};classDerived:publicBase{public:~Derived();voidother_functionality(){//somecode};};现在如果我这样做:intmain(){Base*P=newDerived();deletep;return0;}报错:删除具有非虚析构函数的多态类类型的对象。但是使用unique_ptr它会毫无警告地通过。intmain(){std::un

c++ - 将 unique_ptr 移入 lambda 时,为什么不能调用 reset?

当将std::unique_ptr移动到lambda中时,无法在其上调用reset(),因为它似乎是const:errorC2662:voidstd::unique_ptr>::reset(int*)noexcept':cannotconvert'this'pointerfrom'conststd::unique_ptr>'to'std::unique_ptr>&#includeintmain(){autou=std::unique_ptr();autol=[v=std::move(u)]{v.reset();//thisdoesn'tcompile};}为什么会这样?是否有可能以另一

c++ - 将 unique_ptr 移入 lambda 时,为什么不能调用 reset?

当将std::unique_ptr移动到lambda中时,无法在其上调用reset(),因为它似乎是const:errorC2662:voidstd::unique_ptr>::reset(int*)noexcept':cannotconvert'this'pointerfrom'conststd::unique_ptr>'to'std::unique_ptr>&#includeintmain(){autou=std::unique_ptr();autol=[v=std::move(u)]{v.reset();//thisdoesn'tcompile};}为什么会这样?是否有可能以另一

c++ - std::unique_ptr 如何没有大小开销?

如果空类的大小不能为0,std::tuple有什么魔力,所以unique_ptr的sizeof在64位机器中返回8?在unique_ptr中,成员定义为:typedefstd::tuple__tuple_type;__tuple_type_M_t;其中_Dp是删除器类。编译器是gcc版本4.7.1(Debian4.7.1-7) 最佳答案 原因是typename_Dp=default_delete是一个空类,tuple模板采用空基类优化。如果您实例化unique_ptr使用非默认删除,您应该会看到大小增加。

c++ - std::unique_ptr 如何没有大小开销?

如果空类的大小不能为0,std::tuple有什么魔力,所以unique_ptr的sizeof在64位机器中返回8?在unique_ptr中,成员定义为:typedefstd::tuple__tuple_type;__tuple_type_M_t;其中_Dp是删除器类。编译器是gcc版本4.7.1(Debian4.7.1-7) 最佳答案 原因是typename_Dp=default_delete是一个空类,tuple模板采用空基类优化。如果您实例化unique_ptr使用非默认删除,您应该会看到大小增加。