草庐IT

Initialization

全部标签

c++ - 对象初始化

在《TheC++Language》一书中,作者声称Sometimes,whenyoudesignalibrary,itisnecessary,orsimplyconvenient,toinventatypewithaconstructorandadestructorwiththesolepurposeofinitializationandcleanup.Suchatypewouldbeusedonceonly:toallocateastaticobjectsothattheconstructorandthedestructorarecalled.我对这个声明指的是哪种场景感兴趣?或者这

c++ - 构建一个 vector 以允许未初始化的存储

假设我想构建一个vector容器,unlikestd::vector,允许未初始化的存储。容器的用法,比如vec,大概是这样的:用户明确声明vector应该像这样分配N个未初始化的元素:veca(N,no_init);在数据已知的某个时刻,用户显式地初始化位置n的元素使用参数args...:a.init(n,args...);或者,等效地,手动构造元素:new(&a[n])T(args...);其他操作可能会进行更大规模的初始化或复制(如std::uninitialized_copy),但这只是为了方便;基本的底层操作是相同的。完成一些任务后,vector可能会留下一些已初始化的元素,

c++ - 值初始化 vs Calloc vs 手动初始化速度

哪个最快?我尝试用这个基本容量测试三种方法的速度:#include"stdafx.h"#include"stdlib.h"#include"stdio.h"#include"time.h"int_tmain(intargc,_TCHAR*argv[]){constunsignedlonglongARR_SIZ=0x4fffffff;clock_tval_init_dur,calloc_dur,manual_dur;clock_tcur=clock();char*val_init=newchar[ARR_SIZ]();clock_tafter=clock();val_init_dur=a

C++ - 如何反复重新初始化对象?

我正在尝试用C++实现这个逻辑:Objectobj(args);while(obj.isOK()){obj=obj.next();}但我不能使用这个确切的代码,因为Object继承了boost::noncopyable所以它没有赋值运算符。我可以向Object添加方法和构造函数(但不能使其可复制),但我不想这样做。其他问题有手动销毁和放置new作为解决方案,如果我为Object创建一个新的构造函数,我可以这样做,但同样,最好我不需要新的构造函数,这看起来像无论如何,这是非常讨厌的解决方案。我有什么选择? 最佳答案 使Object::

c++ - block 范围静态的析构函数可以被调用几次?

我刚刚读了this有关当前boost::mutex实现背后实际原因的文章,并注意到以下短语:Block-scopestaticshavetheadditionalproblemofapotentialraceconditionon"thefirsttimethrough",whichcanleadtothedestructorbeingrunmultipletimesonpopularcompilers,whichisundefinedbehaviour—compilersoftenusetheequivalentofacalltoatexitinordertoensurethatde

c++ - 如何理解在某些情况下允许实现将非局部变量的动态初始化视为静态初始化?

其实问题出在标准草案N4582中的话:[basic.start.static/3]Animplementationispermittedtoperformtheinitializationofavariablewithstaticorthreadstoragedurationasastaticinitializationevenifsuchinitializationisnotrequiredtobedonestatically,providedthat—thedynamicversionoftheinitializationdoesnotchangethevalueofanyothe

c++ - 模板类中的静态变量初始化

谁能解释为什么这段代码会崩溃?使用mingw和ubuntu的两个窗口上的行为相同。每个调试器传递给构造函数One的参数“a”被“优化掉”。当我尝试访问静态成员two_时发生崩溃;三.h#ifndefTHREE_H#defineTHREE_H#includeclassOne{public:One(conststd::string&a):a_(a){}std::stringa_;};templateclassTwo:publicOne{public:Two():One(P::name){}std::stringfoo(){returna_;}};templateclassThree{pub

c++ - [conv]/6中语句 "The expression e is used as a glvalue if and only if the initialization uses it as a glvalue"的确切含义是什么

[conv]/6(重点是我的):Theeffectofanyimplicitconversionisthesameasperformingthecorrespondingdeclarationandinitializationandthenusingthetemporaryvariableastheresultoftheconversion.TheresultisanlvalueifTisanlvaluereferencetypeoranrvaluereferencetofunctiontype([dcl.ref]),anxvalueifTisanrvaluereferencetoob

C++符号分析: how to determine which static initialization is performed?

我想分析是什么原因导致我在Linux上由GCC(v.6.1.1)编译的共享C++库的大小。readelf-sWlibfoo.so告诉我特别大的函数叫做__static_initialization_and_destruction_0,例如:000000000026c42010272FUNCLOCALDEFAULT12__static_initialization_and_destruction_0(int,int)[clone.constprop.1774]我将-Wl,-Map,foo.map添加到CXX标志以生成链接器映射文件。在该映射文件中查找0x000000000026c420会

c++ - 初始化期间的 lambda 捕获应该是一个错误

我正在尝试做的是在构造一个可能无效的对象时吃掉异常。它非常适合使用std::optional,但我不相信省略std::optional会改变我看到的错误:对象正在在初始化之前捕获并使用。我认为首先不应该捕获它,因为据我所知我们还没有达到序列点(lambda初始化算作序列点吗?)。此外,该错误是IMO容易捕获的人为错误(甚至确实会被捕获……视情况而定)。lambda如何(更重要的是,为什么)能够捕获和使用尚未初始化的foo?https://godbolt.org/g/IwcHrV#includeusingnamespacestd;voidfoo(){stringfoo=[&]()->st