草庐IT

ptr_container

全部标签

c++ - 将 unique_ptr 插入 map ,指针被销毁

我有以下(简化的)类(class):classOperator{private:std::map>op;public:templatevoidinsertOperand(std::stringconst&s,To=T()){op.insert(std::pair>(s,std::move(std::unique_ptr(newT(o))));}voidsetOperandsValue(std::stringconst&o,intv){op.find(o)->second->setValue(v);}};插入一个新的Operand没有任何问题。然而,当函数返回时,析构函数被调用,因此ma

c++ - Python -> C++ 习语 : Storing lambda expressions in a map/container

我正在学习C++(通过Qt4)利用我的python/pyqt4经验,但我似乎无法掌握将lambda表达式存储到容器中以用作回调的正确习惯用法。我有一个包含大量字段的结构。我想创建一个回调映射,可以以特定方式正确格式化字段。这是我想做的python等价物:fromPyQt4.QtCoreimportQVariant,QStringclassAType(object):def__init__(self):self.name="FOO"self.attr2="BAR"self.attr3="BAZ"#...callbacks={}callbacks['name']=lambdax:QVari

c++ - QMap 和 std::unique_ptr

我试图防止裸指针,防止内存泄漏等。我还想将int映射到INuiSensor*.由于我也在使用Qt,所以我尝试使用QMap>来执行此操作,但QMap的源代码使这变得不可能:templateQ_INLINE_TEMPLATEtypenameQMap::iteratorQMap::insert(constKey&akey,constT&avalue){detach();Node*n=d->root();Node*y=d->end();Node*last=0;boolleft=true;while(n){y=n;if(!qMapLessThanKey(n->key,akey)){last=n;

C++ ptr_fun 找不到 void 参数化函数

我尝试使用std::ptr_fun来包装我的函数,但是当我尝试使用void参数和bool返回类型来包装一个函数时,我得到了一个错误:代码:std::functioncr=std::not1(std::ptr_fun(&funct1));功能:boolfunct1(){returnfalse;}错误:error:nomatchingfunctionforcallto'ptr_fun(bool(*)())'但每当我将参数更改为int时,问题似乎就消失了。如何使用void参数包装函数? 最佳答案 std::ptr_fun仅适用于一元函数:

c++ - Visual Studio 2013 C++ - 将 std::unique_ptr 传递给绑定(bind)函数

使用VisualStudio2013RC和C++,我尝试将std::unique_ptr传递给已使用std::bind绑定(bind)的函数。但是,我遇到了麻烦,因为当我尝试这个时VS似乎不喜欢它。这是我要编译的内容:#include#include#includevoidfunc(std::unique_ptrarg){std::cout)>bound=std::bind(&func,std::placeholders::_1);std::unique_ptrptr(newint(42));bound(std::move(ptr));return0;}这可以在GCC4.8.1中编译,

c++ - 试图理解 auto_ptr

我试图了解有关auto_ptr类如何工作的某些细节。假设您有以下类(class)(我是在一个网站上找到的,该网站上有人解释了赋值运算符的要点)。classTFoo:publicTSuperFoo{auto_ptrfBar1;auto_ptrfBar2;public:TFoo&TFoo::operator=(constTFoo&that);//variousothermethoddefinitionsgohere...}现在是赋值运算符的实现。TFoo&TFoo::operator=(constTFoo&that){if(this!=&that){auto_ptrbar1=newTBar

c++ - 使用 std::unique_ptr 管理 COM 对象

我正在尝试使用智能指针在我的类中保存COM对象,同时避免使用ComPtr。是否可以为此目的使用unique_ptr?我对智能指针很陌生,到目前为止我有点困惑。请考虑以下简化代码:classTexture{private:structComDeleter{operator()(IUnknown*p){p.Release();deletep;}}ID3D11Texture*m_dumbTexture;std::unique_ptrm_smartTexture;public:ID3D11Texture*getDumbTexture()const{returnm_dumbTexture;}ID

c++ - union 中的 std::shared_ptr

我正在实现一个“变体”类,它必须具有尽可能小的内存占用并使用共享指针机制存储一些对象。为此,我想在所有变量类型的类中建立一个union。这包括一些shared_ptr。operator=和复制构造函数必须更改变量的数据类型,从而切换到union中的另一个成员。切换到shared_ptr后,应将其重置为null而无需删除/取消拥有指针。有办法做到这一点吗?当然,还有其他方法可以实现这一点,但在我的例子中,它们通常更复杂、更不安全或消耗更多内存。不过欢迎提出任何建议。谢谢! 最佳答案 重置为null是不够的;的实现std::shared

c++ - 使用 unique_ptr 成员编写移动构造函数的正确方法(崩溃)

以下代码在VisualStudio2013下会崩溃我想知道为什么:在这种情况下编写移动构造函数的正确方法是什么?删除移动构造函数解决了这个问题。是VC++的错误还是这段代码有误?移动构造函数的默认定义有何不同,这使得这段代码不会崩溃,而我自己的定义会崩溃?#include#includeclassA{};classFoo{public:Foo(std::unique_ptrref):mRef(std::move(ref)){}Foo(Foo&&other):mRef(std::move(other.mRef)){}Foo(constFoo&other){}Foo&operator=(c

c++ - 将 unique_ptr 引用传递给 boost::bind?

我在CentOS6.6(gcc4.4.7)上使用Boost.Asio(1.41)进行开发。我希望io_service在启动时调用manger对象m中的成员函数run()。我尝试编译的代码如下所示:#include#include#includeboost::asio::io_serviceio;std::unique_ptrm;m=std::make_unique;io.post(boost::bind(&manager::run,&m));gcc对boost::bind语句进行了调整,其中包括:/usr/include/boost/bind/mem_fn_template.hpp:4