Tag: unique ptr

使用类成员的智能指针

我无法理解智能指针在C ++ 11中作为类成员的用法。 我读了很多关于智能指针的信息,我想我明白unique_ptr和shared_ptr / weak_ptr如何工作的。 我不明白的是真正的用法。 似乎每个人都推荐使用unique_ptr作为几乎所有的时间。 但是,我将如何实现这样的东西: class Device { }; class Settings { Device *device; public: Settings(Device *device) { this->device = device; } Device *getDevice() { return device; } }; int main() { Device *device = new Device(); Settings settings(device); // … Device *myDevice = settings.getDevice(); // do something with myDevice… } […]

unique_ptr和shared_ptr之间的区别

可能重复: pimpl:shared_ptr或unique_ptr 智能指针(boost)解释 有人可以解释shared_ptr和unique_ptr之间的区别吗?

从函数返回unique_ptr

unique_ptr<T>不允许复制构造,而是支持移动语义。 然而,我可以从一个函数返回一个unique_ptr<T> ,并将返回的值赋给一个variables。 #include <iostream> #include <memory> using namespace std; unique_ptr<int> foo() { unique_ptr<int> p( new int(10) ); return p; // 1 //return move( p ); // 2 } int main() { unique_ptr<int> p = foo(); cout << *p << endl; return 0; } 上面的代码编译和按预期工作。 那么第1行如何不调用复制构造函数并导致编译器错误呢? 如果我不得不使用第二行,那么它是有道理的(使用第二行也是如此,但我们并不需要这样做)。 我知道C ++ 0x允许这个exception为unique_ptr因为返回值是一个临时对象,一旦函数退出就会被销毁,从而保证了返回指针的唯一性。 我很好奇这是如何实现的,在编译器中是特殊的,还是在语言规范中有一些其他的子句呢?

用unique_ptr复制类的构造函数

如何为具有unique_ptr成员variables的类实现复制构造函数? 我只考虑C ++ 11。

我如何使用一个std :: unique_ptr成员的定制删除器?

我有一个unique_ptr成员的类。 class Foo { private: std::unique_ptr<Bar> bar; … }; Bar是一个具有create()函数和destroy()函数的第三方类。 如果我想在独立函数中使用std::unique_ptr ,我可以这样做: void foo() { std::unique_ptr<Bar, void(*)(Bar*)> bar(create(), [](Bar* b){ destroy(b); }); … } 有没有办法做到这一点与std::unique_ptr作为一个类的成员?

std :: auto_ptr到std :: unique_ptr

随着新的标准的到来(和一些编译器已经可用的部分),新的std::unique_ptrtypes应该是std::auto_ptr的替代品。 他们的使用是否完全重叠(所以我可以在我的代码上进行全局查找/replace(不是我会这样做,但是如果我这样做)),还是应该知道读取文档中不明显的一些差异? 另外,如果它是一个直接replace(为什么给它一个新的名字),而不是改善std::auto_ptr 。

如何在C ++ 11中实现make_unique函数?

我的编译器不支持make_unique。 如何写一个? template< class T, class… Args > unique_ptr<T> make_unique( Args&&… args );

如何捕获一个lambdaexpression式unique_ptr?

我已经尝试了以下内容: std::function<void ()> getAction(std::unique_ptr<MyClass> &&psomething){ //The caller given ownership of psomething return [psomething](){ psomething->do_some_thing(); //psomething is expected to be released after this point }; } 但它不编译。 有任何想法吗? 更新: 如上所示,需要一些新的语法明确指定我们需要将所有权转移给lambda,我现在想的是下面的语法: std::function<void ()> getAction(std::unique_ptr<MyClass> psomething){ //The caller given ownership of psomething return [auto psomething=move(psomething)](){ psomething->do_some_thing(); //psomething is expected to be released after this point }; } 会不会是个好人选? […]

“downcasting”unique_ptr <Base>到unique_ptr <Derived>

我有一系列返回unique_ptr<Base>的工厂。 然而,它们提供了指向各种派生types的指针,即unique_ptr<Derived> , unique_ptr<DerivedA> , unique_ptr<DerivedB>等。 鉴于DerivedA : Derived和Derived : Base我们会有: unique_ptr<Base> DerivedAFactory() { return unique_ptr<Base>(new DerivedA); } 我需要做的就是将指针从返回的unique_ptr<Base>到某个派生级别(不一定是原来的内部级别)。 为了说明伪代码: unique_ptr<Derived> ptr = static_cast<unique_ptr<Derived>>(DerivedAFactory()); 我正在考虑通过从unique_ptr释放对象,然后使用一个函数来转换原始指针并将其重新分配给另一个所需flavor的unique_ptr ( release将在调用之前由调用方明确完成): unique_ptr<Derived> CastToDerived(Base* obj) { return unique_ptr<Derived>(static_cast<Derived*>(obj)); } 这是有效的,还是会有一些质朴的事情呢? PS。 还有一个复杂的问题,一些工厂驻留在运行时dynamic加载的DLL中,这意味着我需要确保生成的对象在创build时在相同的上下文(堆空间)中被销毁。 所有权的转移(通常发生在另一个环境中)必须从原始上下文中提供一个删除者。 但是除了必须提供/删除指针以外,投射问题应该是相同的。

具有不完整types的std :: unique_ptr将不能编译

我用std::unique_ptr使用pimpl-idiom: class window { window(const rectangle& rect); private: class window_impl; // defined elsewhere std::unique_ptr<window_impl> impl_; // won't compile }; 但是,在<memory>第304行中,出现了有关使用不完整types的编译错误: “ sizeof ”应用于不完整的types“ uixx::window::window_impl ” 据我所知, std::unique_ptr应该能够使用一个不完整的types。 这是一个在libc ++中的错误,或者我在这里做错了什么?