草庐IT

together_unique

全部标签

c++ - unique_ptr 和多态性

我有一些当前使用原始指针的代码,我想更改为智能指针。这有助于以各种方式清理代码。无论如何,我有返回对象的工厂方法以及调用者管理它们的责任。所有权不共享,所以我认为unique_ptr是合适的。我返回的对象通常都派生自一个基类Object。例如,classObject{...};classNumber:publicObject{...};classString:publicObject{...};std::unique_ptrState::NewNumber(doublevalue){returnstd::unique_ptr(newNumber(this,value));}std::u

c++ - unique_ptr 和多态性

我有一些当前使用原始指针的代码,我想更改为智能指针。这有助于以各种方式清理代码。无论如何,我有返回对象的工厂方法以及调用者管理它们的责任。所有权不共享,所以我认为unique_ptr是合适的。我返回的对象通常都派生自一个基类Object。例如,classObject{...};classNumber:publicObject{...};classString:publicObject{...};std::unique_ptrState::NewNumber(doublevalue){returnstd::unique_ptr(newNumber(this,value));}std::u

c++ - unique_ptr、pimpl/forward 声明和完整定义

我已经查看了问题here和here,但仍然无法找出问题所在。这是调用代码:#include"lib.h"usingnamespacelib;intmain(constintargc,constchar*argv[]){return0;}这是库代码:#ifndeflib_h#definelib_h#include#include#includenamespacelib{classFoo_impl;classFoo{public:Foo();~Foo();private:Foo(constFoo&);Foo&operator=(constFoo&);std::unique_ptrm_imp

c++ - unique_ptr、pimpl/forward 声明和完整定义

我已经查看了问题here和here,但仍然无法找出问题所在。这是调用代码:#include"lib.h"usingnamespacelib;intmain(constintargc,constchar*argv[]){return0;}这是库代码:#ifndeflib_h#definelib_h#include#include#includenamespacelib{classFoo_impl;classFoo{public:Foo();~Foo();private:Foo(constFoo&);Foo&operator=(constFoo&);std::unique_ptrm_imp

c++ - 为什么 C++14 中没有 std::allocate_unique 函数?

为什么shared_ptr有allocate_shared而unique_ptr没有allocate_unique?我想使用我自己的分配器创建一个unique_ptr:我必须自己分配缓冲区然后将它分配给一个unique_ptr吗?这似乎是一个明显的习语。 最佳答案 doIhavetoallocatethebuffermyselfandthenassignittoaunique_ptr?不仅仅是一个缓冲区,一个指向对象的指针。但是对象可能需要被分配器销毁,内存肯定需要被分配器释放,所以你还需要将unique_ptr传递给分配器。它不知

c++ - 为什么 C++14 中没有 std::allocate_unique 函数?

为什么shared_ptr有allocate_shared而unique_ptr没有allocate_unique?我想使用我自己的分配器创建一个unique_ptr:我必须自己分配缓冲区然后将它分配给一个unique_ptr吗?这似乎是一个明显的习语。 最佳答案 doIhavetoallocatethebuffermyselfandthenassignittoaunique_ptr?不仅仅是一个缓冲区,一个指向对象的指针。但是对象可能需要被分配器销毁,内存肯定需要被分配器释放,所以你还需要将unique_ptr传递给分配器。它不知

c++ - clang 3.1 看不到 unique_ptr?

我刚刚开始玩clang并尝试编译以下示例程序:#include#includeintmain(){std::unique_ptru(newunsigned(10));std::cout编译时出现以下错误:$clang++helloworld.cpphelloworld.cpp:6:10:error:nomembernamed'unique_ptr'innamespace'std'std::unique_ptru(newunsigned(10));~~~~~^helloworld.cpp:6:29:error:expected'('forfunction-stylecastortypec

c++ - clang 3.1 看不到 unique_ptr?

我刚刚开始玩clang并尝试编译以下示例程序:#include#includeintmain(){std::unique_ptru(newunsigned(10));std::cout编译时出现以下错误:$clang++helloworld.cpphelloworld.cpp:6:10:error:nomembernamed'unique_ptr'innamespace'std'std::unique_ptru(newunsigned(10));~~~~~^helloworld.cpp:6:29:error:expected'('forfunction-stylecastortypec

c++ - 为什么 unique_ptr::reset 没有带删除器的重载?

unique_ptr::reset没有使用constdeleter&和deleter&&来匹配其构造函数的重载是否有原因?那些作为第二个论点?unique_ptr中存储的删除器将使用来自reset的参数进行复制分配或移动分配。如果删除器不可复制或不可移动,则调用reset的相应重载将无法编译。这似乎与构造函数的行为一致。 最佳答案 我考虑过添加它,但您可以使用移动赋值运算符获得等效功能:ptr=unique_ptr(newT(another_value),D(another_state));所以我选择不使用reset说同样的话,以保

c++ - 为什么 unique_ptr::reset 没有带删除器的重载?

unique_ptr::reset没有使用constdeleter&和deleter&&来匹配其构造函数的重载是否有原因?那些作为第二个论点?unique_ptr中存储的删除器将使用来自reset的参数进行复制分配或移动分配。如果删除器不可复制或不可移动,则调用reset的相应重载将无法编译。这似乎与构造函数的行为一致。 最佳答案 我考虑过添加它,但您可以使用移动赋值运算符获得等效功能:ptr=unique_ptr(newT(another_value),D(another_state));所以我选择不使用reset说同样的话,以保