草庐IT

static-initializer

全部标签

c++ - static constexpr 指向函数的指针,编译器之间的区别

回答thisquestion时,我用gcc(codecompiled)和clang(coderejected)尝试了以下代码:typedeflong(*func)(int);longfunction(int){return42;}structTest{staticconstexprfuncf=&function;};templatestructCall{staticvoidf(){c(0);}};intmain(){Call::f();}我不确定哪个编译器是正确的,虽然我认为Test::f的constexpr初始化是可以的。错误clang输出是:error:non-typetempla

c++ - 使用 std::initializer_list 作为成员变量

这个问题在这里已经有了答案:Canitbesafetokeepacopyofanstd::initializer_list?Whatistherationale?(1个回答)关闭4年前。我有一个A类,它接受一个initializer_list并将其存储为一个成员变量。classA{public:A(std::initializer_listil):m_il(il){}std::initializer_listm_il;};另一个类B将A作为成员变量,默认使用initializer_list初始化classB{public:B(){std::cout现在,当我在main中运行这段代码时,

c++ - 错误 : Qualifiers dropped in binding reference of type x to initializer of type y

为什么下面会抛出这个错误:IntelliSense:qualifiersdroppedinbindingreferenceoftype"string&"toinitializeroftype"conststring".hclassA{public:wstring&GetTitle()const;private:wstringtitle;};.cppwstring&GetTitle()const{returnthis->title;}如果我删除const词,它就会停止提示,但我从未对变量进行任何更改? 最佳答案 通过返回对类成员的非c

c++ - initializer_list c++11 中的评估顺序

在下面的代码中,是否要求在f2之前调用f1(或反之亦然),还是未指定?intf1();intf2();std::initializer_listlist{f1(),f2()}; 最佳答案 这是C++标准的一个有趣的角落,其中执行顺序定义明确。第8.5.4节[dcl.init.list],第4段:Withintheinitializer-listofabraced-init-list,theinitializer-clauses,includinganythatresultfrompackexpansions(14.5.3),aree

c++ - static_cast 的指针值

在当前标准草案(和C++17)中,this是关于static_castingvoid*的:Aprvalueoftype“pointertocv1void”canbeconvertedtoaprvalueoftype“pointertocv2T”,whereTisanobjecttypeandcv2isthesamecv-qualificationas,orgreatercv-qualificationthan,cv1.IftheoriginalpointervaluerepresentstheaddressAofabyteinmemoryandAdoesnotsatisfytheali

c++ - 用于确保设计契约(Contract)的 static_assert

作为开发人员团队的一员,我想确保在我们发布的自定义迭代器上实现一组函数(和运算符)。使用STL迭代器类型作为基类型会有所帮助,但是由于某些原因(超出我的控制范围),我们决定不强制执行STL兼容性。迭代器由同一个团队和整个公司的人员使用。我想设计一个使用迭代器类型并根据设计契约进行测试的模板类。例如,我希望迭代器实现operator++、operator--并声明所需的typedef。1>是否可以实现这样一个强制设计契约的模板类?可能使用static_assert?2>如果是,这是一个好的设计吗?引用:customiterator 最佳答案

c++ - reinterpret_cast 与 static_cast 用于在标准布局类型中写入字节?

我需要写入某些整数类型的单个字节。我应该使用reinterpret_cast,还是应该通过void*使用static_cast?(一)unsignedshortv16;char*p=static_cast(static_cast(&v16));p[1]=...somecharvaluep[0]=...somecharvalue或(b)unsignedshortv16;char*p=reinterpret_cast(&v16);p[1]=...somecharvaluep[0]=...somecharvalue根据static_castandreinterpret_castforstd:

c++ - const static auto lambda 与引用捕获一起使用

在C++11函数中使用一些本地lambda对象时,我很想将它们声明为conststaticautolambda=...只是为了让编译器知道只有一个std::function需要对象(并可能优化调用和/或内联它),但我意识到在这种情况下通过引用捕获局部值会导致奇怪的行为。考虑以下代码:voidprocess(constData&data,conststd::function&lambda){...}voidSomeClass::doSomething(){intfoo=0;conststaticautolambda=[&foo](){....++foo;....}process(data

c++ - 与 -static 链接时出现 Valgrind 错误——为什么?

我有一个测试驱动程序链接到我编写的库。该库使用autotools,因此它会生成存档(.a文件)和动态库(.so)。当我将我的驱动程序与“g++-static”链接时,大概是链接到.a,valgrind点亮并反复提示“条件跳转或移动取决于未初始化的值”。第一次失败发生在__pthread_initialize_minimal中的main之前。当我在没有-static的情况下进行链接时,大概是在使用.so进行链接时,我没有收到任何valgrind投诉。有人知道为什么吗?valgrind是否不能与-static一起使用?更新:我什至无法发布我的驱动程序的精简版本,因为它链接到一个我无法精简的

c++ - 为什么在这里使用 static_cast 而不是 reinterpret_cast 很重要?

AtareplyofablogpostofRaymondChen,提问者指出Raymond,IbelievetheC++exampleisnotcorrectsincethepositionofthebaseclasssubobjectinthederivedclassisunspecifiedaccordingtoISOC++2003Standard(10-3,page168),andyouassumethatthebaseclasssubobjectisalwaysatthebeginning.TheCexamplewouldbefineinC++too,soI'dstickwit