草庐IT

static-initializer

全部标签

c++ - 将 Variadic 模板包转换为 std::initializer_list

假设有一个接受多个字符串的函数:voidfun(conststd::initializer_list&strings){for(autos:strings)//dosomething}现在,我有一个可变参数template函数说foo()为:templatevoidfoo(){fun(???);}这个方法被外部调用为:foo();//whereA,B,C,Dareclasses这些作为参数传递的类应该包含一个共同的staticconst成员:staticconststd::stringvalue="...";这是我的问题(如何):在foo()中,检查是否所有的Args都包含value使

C++11 move(x) 实际上意味着 static_cast<X&&>(x)?

这个问题在这里已经有了答案:Whenisthemoveconstructorcalledinthe`std::move()`function?(2个答案)关闭9年前。刚刚阅读了Stroustrup的C++编程语言第4版,在第7章中他说:move(x)meansstatic_cast(x)whereXisthetypeofx和Sincemove(x)doesnotmovex(itsimplyproducesanrvaluereferencetox)itwouldhavebeenbetterifmove()hadbeencalledrval()我的问题是,如果move()只是将变量转换为r

initialization - Kotlin val 初始化使用 when

使用Java我可能想使用switch语句初始化最终变量:finalStringfinalValue;switch(condition){case1:finalValue="One";break;case2:finalValue="Two";break;case3:finalValue="Three";break;default:finalValue="Undefined";break;}在Kotlin中,尝试做同样的事情:valfinalValue:Stringwhen(condition){1->finalValue="One"2->finalValue="Two"3->finalV

initialization - Kotlin val 初始化使用 when

使用Java我可能想使用switch语句初始化最终变量:finalStringfinalValue;switch(condition){case1:finalValue="One";break;case2:finalValue="Two";break;case3:finalValue="Three";break;default:finalValue="Undefined";break;}在Kotlin中,尝试做同样的事情:valfinalValue:Stringwhen(condition){1->finalValue="One"2->finalValue="Two"3->finalV

c++ - 在 Windows 上将 Boost 库与 Boost_USE_STATIC_LIB OFF 链接

我的CMakeFiles.txt看起来像这样:cmake_minimum_required(VERSION2.6)#SetwarningsonandenabledebuggingSET(CMAKE_C_FLAGS"-Wall-q")include(FindBoost)set(Boost_USE_STATIC_LIBSON)set(Boost_USE_MULTITHREADEDON)set(Boost_USE_STATIC_RUNTIMEOFF)find_package(Boost1.57.0COMPONENTSsystemfilesystemREQUIRED)if(Boost_FOUN

c++ - 奇怪的 static_cast 把戏?

在仔细阅读Qt源代码时,我遇到了这个gem:templateinlineTqgraphicsitem_cast(constQGraphicsItem*item){returnint(static_cast(0)->Type)==int(QGraphicsItem::Type)||(item&&int(static_cast(0)->Type)==item->type())?static_cast(item):0;}注意static_cast(0)->Type?我已经使用C++很多年了,但以前从未见过0在static_cast中使用过。这段代码在做什么,它安全吗?背景:如果您从QGrap

c++ - 私有(private)成员 : Static const vs. 只是 const

当一个对象具有一些不会改变并且在其整个功能中都需要的特征时,我正在尝试确定最佳选择。静态常量成员Const成员在我看来,静态成员的真正原因是拥有一个可以更改的变量,从而影响同一类的所有其他对象。但是,有人建议将类“不变量”作为静态常量成员。我正在寻找有关建立类常量的推荐方法及其原因的一些见解。 最佳答案 “不会改变”不够准确。这里的主要问题是类的不同对象是否需要具有这些const成员的不同值(即使它们在对象的生命周期内没有改变)或者所有对象都应该使用(共享)相同的值。如果类的所有对象的值都相同,那么当然应该是类的staticcons

c++ - 'default-initialization in copy-initialization context' 在 C++ 中是什么意思?

例如,我想我理解直接初始化(与复制)上下文中的列表初始化意味着什么-intx{}与intx={}基本上。但是在cppreference我发现了这个:Whenanobjectofclasstypeiscopy-initializedfromanobjectofthesameorderivedclasstype,ordefault-initializedinacopy-initializationcontext,thecandidatefunctionsareallconvertingconstructorsoftheclassbeinginitialized.Theargumentlis

c++ - g++ 上 constexpr 上下文中成员指针的 static_cast

我在使用static_cast在constexpr上下文中向上转换成员指针时遇到了g++问题。请参见代码示例。在使用g++6.3和7.0版进行编译时,会出现编译错误,指出reinterpret_cast不是常量表达式。虽然clang4.0版没有给出错误,但我认为这是正确的,因为这里没有reinterpret_cast。这是g++或clang中的错误吗?什么是正确的行为?structBase{};structDerived:Base{inti;};structPtr{constexprPtr(intDerived::*p):p(static_cast(p)){}intBase::*p;}

c++ - #include <initializer_list> 需要在基于范围的情况下使用初始化列表吗?

最终的C++11标准包括对基于范围的for的规定,以便在不必包含的情况下“仅适用于”native数组。或任何其他标题。据我所知,这首先在工作文件n2900中得到解决。作为评论UK78和79的结果。该提案还包括一项隐式规定#include在每个翻译单元中,例如程序#includeintmain(){for(autoi:{1,2,3,4,5})std::cout即使不包含也会符合标准.然而,当概念从C++11中删除时,基于范围的for被修改,如n2930中所示.虽然数组“正常工作”的规定仍然存在,但没有提到初始化列表也是如此;确实是各种标准库容器头文件的规范#include而8.5.4.2