草庐IT

Initialization

全部标签

c++ - 在结构中初始化数组

假设我们有一些模板化结构,有时它的模板应该是一个数组。如何在struct中初始化数组?这个templatestructA{Tx;A(Tx):x(x){}};inta[6];Ab(a);在编译期间产生错误:error:arrayinitializermustbeaninitializerlistA(Tx):x(x){}^UPD1.这个东西用在更完整的代码中:templatestructA{Tx;A(constT&x):x(x){}A(constT&&x):x(std::move(x)){}};templateA::type>make_A(T&&a){returnA::type>(std:

c++ - 试图强制静态对象初始化

我试图初始化一个静态对象但没有成功。目的是在存储库中自动注册一个工厂类(这是一个单例)。我已经看过了:Howtoforceastaticmembertobeinitialized?其中一条评论说(我也遵循了一个示例):IreaditupintheC++standard(14.7.1):Unlessamemberofaclasstemplateoramembertemplatehasbeenexplicitlyinstantiatedorexplicitlyspecialized,thespecializationofthememberisimplicitlyinstantiatedwh

c++ - constexpr 初始化是否应该在其他初始化之前发生

我有以下代码,它在gcc和clang上的表现符合预期。然而,MSVC给了我意想不到的结果。让我们先看看有问题的代码。#include//-----------------------------------------------classTest//DummyforMCVE{public:Test();voidPrint();private:intarr[5];};Testtst;//-----------------------------------------------templatestructrange//somestuffnotneededbyexampleremove

C++:const-从一个初始化函数初始化多个数据成员

我有一个带有两个数据成员的C++类,例如,classmytest(){public:mytest():a_(initA()),b_(initB()){};virtual~mytest(){};private:doubleinitA(){//somecomplexcomputation}doubleinitB(){//someothercomplexcomputation}private:constdoublea_;constdoubleb_;}不幸的是,initA和initB不能如上图所示分开。a_和b_都可以通过一次复杂的大计算来初始化,其中b_的值取决于计算的中间结果>a_,例如,

c++ - pre-main 全局初始化程序是否保证运行单线程?

例如,以下代码段中的node::node()构造函数访问全局变量node::count和::tail没有任何多线程保护。C++标准是否保证输出始终是012的排列(无论顺序如何)?#includestructnode*tail;structnode{staticintcount;intindex;node*prev;node(){index=count++;prev=tail;tail=this;}};intnode::count;nodeone,two[2];intmain(intargc,char*argv[]){for(node*p=tail;p;p=p->prev)printf(

c++ - 初始化模板类的静态成员

我想弄清楚为什么这个例子不能编译。我的理解是,如果未显式设置静态变量,则它默认为0。在下面的五个示例中,其中四个的行为符合我的预期,但被注释掉的那个将无法编译。#includeclassFoo{public:staticinti;staticintj;};templateclassBar{public:Bar(int){}staticinti;};staticinti;intFoo::i;intFoo::j=1;templateintBar::i;templateintBar::i=3;intmain(intargc,char**argv){std::cout::i"::i::i"::

c++ - 用户定义类型的数组成员初始化

g++4.7支持数组成员初始化,我开始玩了。下面的代码无法编译。structA{A(int){};A(constA&)=delete;A&operator=(constA&)=delete;~A(){};};structB{B():a{{0},{1}}{};Aa[2];};Bb;gcc4.8(预发布版)的错误信息是:n.cc:Inconstructor‘B::B()’:n.cc:12:20:error:useofdeletedfunction‘A::A(constA&)’a{{0},{1}}^n.cc:4:8:error:declaredhereA(constA&)=delete;^有

c++ - POD的零初始化

structLine{Boundsbounds_;Vectororigin_;uint32_tbegin_;uint32_tend_;distascent_;distdescent_;};使用方法如下:Lineline={};while(!parser.done()){line=Line();//zero-initialize...}Bounds和Vector是非POD类,dist是int64_t的类型定义。但是,优化的32位VC++11发布版本似乎在while循环中至少留下了line的部分未初始化。为什么?根据Dotheparenthesesafterthetypenamemakea

c++ - 如何初始化const循环引用成员

比如我有两个类classFoo;classBar;classFoo{constBar&m_bar;...};classBar{constFoo&m_foo;...};让foo是Foo的对象和bar是Bar的对象.有什么方法(正常或“黑客”)来创建/初始化foo和bar他们的成员m_bar和m_foo会互相引用(我的意思是foo.m_bar是bar而bar.m_foo是'foo')?允许添加任何成员到Foo和Bar,为他们添加parent,使他们成为模板等等。 最佳答案 foo和bar的联系是什么?如果他们有外部链接,你可以这样写:e

模板类型的 C++ Constexpr 成员

我想创建一个模板类,其成员是一个constexpr数组。当然,数组需要根据它的类型进行不同的初始化,但我不能在不初始化数组的情况下声明它。问题是在模板专门化之前我不知道数组的值。//A.hpptemplateclassA{public:staticconstexprTa[];constexprA(){};~A(){};}//B.hppclassB:publicA{public:constexprB();~B();};//B.cpptemplateconstexprintA::a[]={1,2,3,4,5};B::B(){}B::~B(){}如何正确初始化B中的A::a[]?