草庐IT

auto_ptr

全部标签

c++ - shared_ptr 的静态成员函数 make_shared

使用libc++我在公共(public)部分找到了std::shared_ptr::make_shared()静态成员函数。当我已经为std::shared_ptr的特化定义了类型别名时,这非常方便:usingT=int;usingP=std::shared_ptr;autop=P::make_shared(123);//std::make_shared(123)static_assert(std::is_same::value);我担心标准合规性,因为来自可信来源的文章(1,2)没有提到std::shared_ptr的静态成员函数make_shared>.目前使用该功能是否不好?为什

c++ - 模板类中 auto 的不完整类使用

下面的代码格式是否正确?classB;templateclassA{Bdo_f()const;friendautof(Aconst&a){returna.do_f();}//#1};classB{};templateBA::do_f()const{returnB{};}intmain(){Aa;f(a);}如果我将#1​​中的auto更改为B,我会收到不完整的类型错误消息。为gcc/clang使用auto编译DemoB失败Demo 最佳答案 [dcl.fct.def.general]/2:Thetypeofaparameterort

c++ - 在 map 中使用 unique_ptr 时删除 std::pair 中的函数

我有一段C++代码,我不确定它是否正确。考虑以下代码。#include#include#includeusingnamespacestd;intmain(intargc,char*argv[]){vector>>v;v.resize(5);returnEXIT_SUCCESS;}GCC编译这段代码没有问题。然而,英特尔编译器(版本19)因错误而停止:/usr/local/[...]/include/c++/7.3.0/ext/new_allocator.h(136):error:function"std::pair::pair(conststd::pair&)[with_T1=cons

c++ - `auto` 的 ref- 和 cv-stripping 属性。

我学会了以这种方式使用auto声明一个变量autovar=expr;基本上就像获取expr的类型并从中剥离&/&&-references和所有顶级常量和volatile。这是否意味着上面的行完全等同于下面的行?std::remove_cv::type>::typevar=expr; 最佳答案 不,那不是真的。autovar=expr;更像是传递expr按值(value)。intx[1];autoy=x;这使得y一个int*.主要是autox=expr;表现得像模板类型推导:templatevoidf(T);intx[1];f(x);

c++ - C++1 7's deduced ` auto` 非类型 `template` 参数是否可以使用显式非类型参数模式匹配模板?

考虑这个例子(alsoavailableonwandbox):templateclass>voidtest(){}templatestructX{};正在尝试实例化test()在clang++4.0(trunk)导致编译错误:error:nomatchingfunctionforcallto'test'test();^~~~~~~note:candidatetemplateignored:invalidexplicitly-specifiedargumentfor1sttemplateparametervoidtest(){}我最初的假设/直觉是test可用于匹配任何template具

C++ 使用 C++11 (std::shared_ptr) 分配 shared_ptr:将 shared_ptr 初始化为临时变量是否仍然不好?

我正在阅读thisanswer作者指的是boostbestpractices其中说:Avoidusingunnamedshared_ptrtemporariestosavetyping;toseewhythisisdangerous,considerthisexample:voidf(shared_ptr,int);intg();voidok(){shared_ptrp(newint(2));f(p,g());}voidbad(){f(shared_ptr(newint(2)),g());}Thefunctionokfollowstheguidelinetotheletter,wher

c++ - Pimpl with smart ptr - 为什么需要构造函数/析构函数

这个问题在这里已经有了答案:std::unique_ptrwithanincompletetypewon'tcompile(7个答案)关闭8年前。让我们考虑以下示例(使用c++11)A.hpp:#includeclassA{public://A();//~A();private:structAImpl;std::unique_ptrpImpl;};主要.cpp:#include"A.hpp"intmain(){Aa;}使用默认构造函数和析构函数。不编译。发生以下错误:Infileincludedfrom/usr/include/c++/4.8/memory:81:0,fromA.hpp

c++ - 图节点父列表中的 weak_ptr VS shared_ptr

我有一个由Graph和Node类实现的有向无环图。每个节点都有一个指向子节点的指针列表和一个指向父节点的指针列表。我最近添加了父级,因为一些算法需要快速访问父级列表,而且图很小,每个节点只有很少的连接,所以没有内存问题。子列表使用std::shared_ptr以便节点至少在它们有父节点时保留在内存中。但是我不希望节点拥有它的父节点,所以我使用weak_ptr作为指向父节点的指针。但是后来算法出了问题。算法必须从weak_ptr创建一个新的shared_ptr,所以我不能直接使用operator==,并且使用标准函数如std::find()需要编写一个调用my_weak_ptr.lock

c++ - 什么是auto_ptr_ref,它实现了什么以及如何实现的

auto_ptr_ref文档here说这个这是一个工具类,允许进行某些转换,这些转换允许将auto_ptr对象传递给函数并从函数返回。有人可以解释一下auto_ptr_ref如何帮助实现这一目标。我只想了解auto_ptr类及其内部结构 最佳答案 这有点令人困惑。基本上,auto_ptr_ref存在是因为auto_ptr拷贝构造函数并不是标准意义上的拷贝构造函数。复制构造函数通常有一个如下所示的签名:X(constX&b);auto_ptr复制构造函数的签名如下所示:X(X&b)这是因为auto_ptr需要修改从中复制的对象,以便将

c++ - 对 boost::timer::auto_cpu_timer 的 undefined reference

我尝试使用g++4.4在Debian的远程服务器上使用boost库编译小型.cpp文件。为此,我使用Netbeans。我的家用机器在Windows7上。解决了一些链接下一个代码的问题后#include#include#includeintmain(){boost::timer::auto_cpu_timerac;//line5return0;//line6}产生2个错误:第5行:对boost::timer::auto_cpu_timer::auto_cpu_timer(short)'的undefinedreference第6行:对boost::timer::auto_cpu_timer