草庐IT

auto_ptr_ref

全部标签

c++ - std::thread constructor 传递指针和传递ref有区别吗?

创建调用成员函数的线程时,传递当前类的指针和传递引用有区别吗?从下面的示例中,方法1的行为是否与方法2相同?有什么区别吗?classMyClass{public:MyClass(){};~MyClass(){};voidmemberFunction1(){//method1std::threadtheThread(&MyClass::memberFunction2,this,argumentToMemberFunction2)//method2std::threadtheThread(&MyClass::memberFunction2,std::ref(*this),argumentT

c++ - 与普通指针相比,按值传递 `unique_ptr` 是否会降低性能?

Commonwisdomisthatstd::unique_ptrdoesnotintroduceaperformancepenalty(andnotamemorypenaltywhennotusingadeleterparameter),但我最近偶然发现了一个讨论,该讨论表明它实际上引入了一个额外的间接寻址,因为unique_ptr无法在具有ItaniumABI的平台上的寄存器中传递。发布的示例类似于#includeintfoo(std::unique_ptru){return*u;}intboo(int*i){return*i;}Whichgeneratesanadditional

c++ - 从 unique_ptr<T[]> 初始化 shared_ptr<T>

[跟进this问题]最近我一直在处理指向C风格数组的智能指针。我最终完成了推荐的事情并改为使用指向vector的智能指针,但在那段时间里,我得到了一些建议:不要使用shared_ptr对象来管理最初使用make_unique创建的数组因为它不会调用delete[]而是delete.这对我来说似乎不合逻辑,我检查了两个Coliru和标准:这段代码:#include#includeintmain(){std::coutmyUnique(customArrayAllocator(4),customArrayDeleter);std::coutmyShared=std::move(myUniq

c++ - 具有自定义删除器的 unique_ptr 构造函数被删除

这个例子在gcc4.8.3下编译和运行良好:#include#include#includeintmain(){autostr=newconstchar[6]{'h','e','l','l','o','\0'};std::unique_ptr>u_ptr(str,[](constchar*s){delete[]s;});std::cout但是当我尝试使用VisualStudioProfessional2013时,它无法编译(提示函数已删除)。这在VisualStudio2013中还不可能吗?还是我的示例代码有误而gcc忽略了我的错误?错误是:main.cpp(8):errorC2280

C++:将像 unique_ptr::get() 这样的参数传递给函数是否安全

传递像getAName(getA().get())这样的函数参数是安全的?getA()返回一个对象unique_ptr.我在VS2010上测试了下面的整个代码,它有效。但我想确定它是否是c++标准,与其他c++编译器一起使用是否安全?#include"stdafx.h"#include#includeusingnamespacestd;classA{public:A(){coutgetA(){returnstd::unique_ptr(newA());;}voidgetAName(A*a){if(a){coutname().c_str()控制台的输出是:A()A~()为了所有编译器的安

c++ - 在 C 接口(interface)中使用 shared_ptr?

我有一个要移植到C++的C库,它大量使用手动引用计数结构。我考虑过使用shared_ptr来自动处理引用计数,但我还想维护CAPI。旧签名看起来像这样:Object*object_create(void);Object*object_retain(Object*o);voidobject_release(Object*o);如果我使用shared_ptr,有什么方法可以有效地在CAPI中公开此手动引用计数? 最佳答案 shared_ptr的问题,正如您已经发现的那样,您不能修改引用计数,除非通过构造或销毁实例。所以不,除了为每个构造

C ++功能签名接受LVALUE,RVALUE和RVALUE REF

假设我具有以下功能:print_sutff(conststd::stringstuff){std::cout我可以接受:std::stringstd::string&std::string&&对功能代码的外观没有任何影响。但是,可以通过多种方式调用该功能,我想通过这些方式实现以下行为(或尽可能接近它):autopass="astring";print_sutff(pass);在这里,用户不能使用&amp;&amp;我更喜欢&amp;优先考虑lvaue或者:print_sutff("astring");这里是&amp;amp;应调用构造函数。所以我的问题是:有什么方法可以接受lvalue,&am

c++ - 使用 auto 重载模板函数的解析

使用以下3个重载templateautofoo(){return1;}templateintfoo(){return2;}templateTfoo(){return3;}以下是病态的吗?static_cast(&foo)();Clang选择重载#2,而gcc编译失败(Demo)当删除重载#1时,双方都同意选择重载#2(Demo)。删除重载#2时,gcc选择重载#1并且clang编译失败(Demo) 最佳答案 根据[over.over]/2,我们执行模板参数推导。这对于所有三个重载都会成功:在第一个重载中,保留[temp.deduct

c++ - C++14 中 decltype(auto) 的转换函数

classA{public:intnum;A(intparam):num(param){}operatordecltype(auto)(){returnnum;}};classB{public:intnum;AobjA;B(intparam):num(param),objA(param){}//operatorA(){returnobjA;}//Works//#1//operatorint(){returnobjA;}//Works//#2//operatorchar(){returnobjA;}//ActuallyNotNeeded//#3//operatordouble(){ret

c++ - 查找 boost::shared_ptr 循环引用

是否有查找shared_ptr的循环引用的任何提示/技巧?这是我要查找的示例-不幸的是,我似乎无法在我的代码中找到循环。structA{boost::shared_ptranC;};structB{boost::shared_ptranA;};structC{boost::shared_ptranB;}; 最佳答案 我建议使用Valgrind.当您关闭进程时,它会显示所有泄漏的内存。除非你的关机以某种方式打破了循环,否则任何循环都应该显示为内存泄漏,Valgrind会告诉你内存最初是从代码中的哪个位置分配的。