在boost库中是否有一些与C++ 1x的std :: unique_ptr等效的类?我正在寻找的行为是能够拥有异常安全的工厂功能,就像这样......
std::unique_ptr<Base> create_base()
{
return std::unique_ptr<Base>(new Derived);
}
void some_other_function()
{
std::unique_ptr<Base> b = create_base();
// Do some stuff with b that may or may not throw an exception...
// Now b is destructed automagically.
}
Run Code Online (Sandbox Code Playgroud)
编辑:现在,我正在使用这个黑客,这似乎是我能在这一点上得到的最好的......
Base* create_base()
{
return new Derived;
}
void some_other_function()
{
boost::scoped_ptr<Base> b = create_base();
// Do some stuff with b that may or may not throw an exception...
// Now b is deleted automagically.
}
Run Code Online (Sandbox Code Playgroud) 唯一的区别是移动语义boost::scoped_ptr<T>
和移动语义std::unique_ptr<T>
的事实,std::unique_ptr<T>
而boost::scoped_ptr<T>
只是一个获取/重置智能指针?
根据N3290 std::unique_ptr
在其构造函数中接受一个删除参数.
但是,我不能在Windows中使用Visual C++ 10.0或MinGW g ++ 4.4.1,也不能在Ubuntu中使用g ++ 4.6.1.
因此,我担心我对它的理解是不完整或错误的,我不能看到显然被忽略的删除论证的观点,那么任何人都可以提供一个有效的例子吗?
我最好还想看看它是如何工作的unique_ptr<Base> p = unique_ptr<Derived>( new Derived )
.
可能会使用标准中的一些措辞来备份示例,即使用您正在使用的任何编译器,它实际上做了它应该做的事情?
创建包含在免费商店中分配的数组的unique_ptr的正确方法是什么?Visual Studio 2013默认支持这个,但是当我在Ubuntu上使用gcc版本4.8.1时,我得到了内存泄漏和未定义的行为.
使用此代码可以重现此问题:
#include <memory>
#include <string.h>
using namespace std;
int main()
{
unique_ptr<unsigned char> testData(new unsigned char[16000]());
memset(testData.get(),0x12,0);
return 0;
}
Run Code Online (Sandbox Code Playgroud)
Valgrind将给出这个输出:
==3894== 1 errors in context 1 of 1:
==3894== Mismatched free() / delete / delete []
==3894== at 0x4C2BADC: operator delete(void*) (in /usr/lib/valgrind/vgpreload_memcheck-amd64-linux.so)
==3894== by 0x400AEF: std::default_delete<unsigned char>::operator()(unsigned char*) const (unique_ptr.h:67)
==3894== by 0x4009D0: std::unique_ptr<unsigned char, std::default_delete<unsigned char> >::~unique_ptr() (unique_ptr.h:184)
==3894== by 0x4007A9: main (test.cpp:19)
==3894== Address 0x5a1a040 is 0 bytes inside a block of size …
Run Code Online (Sandbox Code Playgroud) 这段代码是否正确?
auto v = make_unique<int>(12);
v.release(); // is this possible?
Run Code Online (Sandbox Code Playgroud)
它等同于delete
原始指针吗?
在C++ 11中,您可以使用a shared_ptr<>
与对象或变量建立所有权关系,并weak_ptr<>
以非拥有方式安全地引用该对象.
您还可以使用unique_ptr<>
与对象或变量建立所有权关系.但是,如果其他非拥有对象想要引用该对象呢?weak_ptr<>
在这种情况下没有帮助.原始指针很有用,但会带来各种缺点(例如,它们可以自动初始化为nullptr,但这是通过与std::*_ptr<>
类型不一致的技术实现的).
什么是对weak_ptr<>
拥有的对象的非拥有引用的等价物unique_ptr<>
?
这是一个澄清的例子,类似于我正在研究的游戏中的某些东西.
class World
{
public:
Trebuchet* trebuchet() const { return m_trebuchet.get(); }
private:
std::unique_ptr< Trebuchet > m_trebuchet;
};
class Victim
{
public:
Victim( Trebuchet* theTrebuchet ) : m_trebuchet( theTrebuchet ) {}
~Victim()
{
delete m_trebuchet; // Duh. Oops. Dumb error. Nice if the compiler helped prevent this.
}
private:
Trebuchet* m_trebuchet; // Non-owning.
};
shared_ptr< Victim > createVictim( World& world …
Run Code Online (Sandbox Code Playgroud) 我一直在使用pimpl成语制作一些对象,但我不确定是否使用std::shared_ptr
或std::unique_ptr
.
据我所知,std::unique_ptr
是更有效的,但是这是没有这么多的问题,对我来说,因为这些物体是比较重量级反正做的成本std::shared_ptr
比std::unique_ptr
相对较小.
我目前std::shared_ptr
只是因为额外的灵活性.例如,使用a std::shared_ptr
允许我将这些对象存储在散列映射中以便快速访问,同时仍然能够将这些对象的副本返回给调用者(因为我相信任何迭代器或引用可能很快变得无效).
但是,这些对象确实没有被复制,因为更改会影响所有副本,所以我想知道也许使用std::shared_ptr
和允许副本是某种反模式或坏事.
它是否正确?
我有一个类型的C++对象 ObjectArray
typedef map<int64_t, std::unique_ptr<Class1>> ObjectArray;
Run Code Online (Sandbox Code Playgroud)
创建一个unique_ptr
新的类型对象Class1
并将其插入到类型的对象中的语法是什么ObjectArray
?
我试图找出如何/我是否可以使用unique_ptr
在queue
.
// create queue
std::queue<std::unique_ptr<int>> q;
// add element
std::unique_ptr<int> p (new int{123});
q.push(std::move(p));
// try to grab the element
auto p2 = foo_queue.front();
q.pop();
Run Code Online (Sandbox Code Playgroud)
我明白为什么上面的代码不起作用.由于front
&pop
是2个单独的步骤,因此无法移动元素.有没有办法做到这一点?
如果我理解正确,a weak_ptr
不会增加托管对象的引用计数,因此它不代表所有权.它只是让您访问一个对象,其生命周期由其他人管理.所以我真的不明白为什么一个weak_ptr
不能用a构建unique_ptr
,而只能用a 构建shared_ptr
.
有人能简单解释一下吗?
c++ ×10
unique-ptr ×10
c++11 ×6
shared-ptr ×2
boost ×1
gcc ×1
linux ×1
map ×1
pimpl-idiom ×1
pointers ×1
scoped-ptr ×1
stl ×1
weak-ptr ×1