草庐IT

static-compilation

全部标签

c++ - 在 for-loop : compiler bug? 中重新排序测试条件

我有一个存储在数组中的树,我正试图找到一个特定的节点:std::vectornodes=...constunsignedshortsentinel=-1;unsignedshortindex=0;for(Node*node=&nodes[index];//rootnodeindex!=sentinel;node=&nodes[index]){if(foo(*node)){index=node->left;}else{index=node->right;}}换句话说,没什么特别的。但是,MSVC2012失败并尝试访问nodes[sentinel]这超出了范围。原来它先计算&nodes[i

c++ - 作为类成员的可变大小数组 : why does it compile?

我有这种情况,我无法解释它编译的原因:#includeusingnamespacestd;classX{public:X(){cout我正在定义一个可变大小的X数组作为类Y的成员。在类外这样定义X肯定会导致编译错误,但在类内不会。更重要的是,X的构造函数从未被调用。那么这里发生了什么? 最佳答案 C99,6.7.2.1/16(n1256)Asaspecialcase,thelastelementofastructurewithmorethanonenamedmembermayhaveanincompletearraytype;thi

c++ - static_cast<T> 对 T& 做了什么?

所以我问了thisquestion我正在尝试通过static_cast解决它。(顺便说一句,它确实解决了问题,我只是不确定我是否理解为什么。)在代码中:vectorfoo={0,42,0,42,0,42};replace(begin(foo),end(foo),static_cast(foo.front()),13);static_cast是否只是构造一个R值int?那和只是打电话有什么区别:replace(begin(foo),end(foo),int{foo.front()},13);编辑:根据答案static_cast的推断,确实似乎构造了一个R值int:http://ideon

c++ - *static* 关键字在成员方法声明中的位置

有什么区别吗classC{staticintfunc();};和classC{intstaticfunc();};我正试图在其他人的代码库中删除关键字static。在我这样做之前,我想确保我理解第二个示例的含义。[编辑]删除static的原因:C是一个没有成员变量且充满静态方法的“类”。我认为让“C”成为具有正常功能的命名空间而不是类更合适。 最佳答案 没有区别。函数声明上的static适用于该函数。this指针不会隐式传递给此函数,因此如果不显式将对象传递给函数,则无法访问此函数内部的非静态类成员。首先要删除static,您应该知

c++ - [C++ 编译时断言] : Can we throw a compilation error if some condition is not met?

我写了一个函数:templatevoidtryHarder(){for(inti=0;i但我只希望它在N介于0和10之间时编译。我可以这样做吗?怎么办? 最佳答案 您可以使用static_assertdeclaration来完成:templatevoidtryHarder(){static_assert(N>=0&&N此功能仅在C++11之后可用。如果您坚持使用C++03,请查看Boost'sstaticassertmacro.整个想法都是很好的错误信息。如果您不关心这些,或者甚至负担不起boost,您可以执行以下操作:templa

C++ : Can the compiler optimize this code segment?

voidfoo(constintconstant){for(inti=0;i外循环的每次执行都会检查“constant”的值。然而,常量永远不会改变,所以大量的CPU时间被浪费在测试条件常量我个人认为这个问题是不可避免的。即使编译器将比较放在外循环之前并设置某种bool变量“skip_inner_stuff”,仍然必须在外循环的每次传递中检查该变量。您对此事有何看法?是否有更有效的方法来编写上述代码段来避免该问题? 最佳答案 您描述的优化也称为loopunswitching.多年来,它一直是优化编译器的标准部分-但如果您想确保编译器

c++ - boost 二进制 static_visitor 和 apply_visitor

我有以下代码:typedefboost::variantSearchParameter;enumVisibility{CLEAR,CLOUDY,FOG,SMOKE};classDetectionGenerator:publicboost::static_visitor{public:DetectionGenerator(constEnvironmentalFactors&factors);doubleoperator()(constLandSearchParameter&land,Visibilityvis)const;doubleoperator()(constWaterSearch

c++ - 这段代码是做什么的 : static union MSVC_EVIL_FLOAT_HACK INFINITY_HACK = {{0x00, 0x00, 0x80, 0x7F}};

#ifndefINFINITY#ifdef_MSC_VERunionMSVC_EVIL_FLOAT_HACK{unsigned__int8Bytes[4];floatValue;};staticunionMSVC_EVIL_FLOAT_HACKINFINITY_HACK={{0x00,0x00,0x80,0x7F}};#defineINFINITY(INFINITY_HACK.Value)#endif我目前正在开始使用Chipmunk物理引擎并在头文件中找到它INFINITY用于为物体设置无限动量,但是我不明白上面这段代码的作用! 最佳答案

c++ - static_assert 和英特尔 C++ 编译器

阅读cppreference.com:Astaticassertdeclarationmayappearatblockscope(asablockdeclaration)andinsideaclassbody(asamemberdeclaration)好的,现在我有以下代码:structfoo_t{staticconstexprstd::size_tmaxAlignment(){//Thisisjustasample;Iremovedrealcodefromthismethod.returnstd::max(alignof(__m128),__alignof(__m256));}sta

c++ - static_assert 无法将 const char* 模板参数识别为 constexpr : g++ bug?

考虑以下定义。charright_string[]="::right_one.";charwrong_string[]="::wrong_one.";templatevoidf(){static_assert(str==::right_string,"Passme::right_string!");}structTest{staticconstexprcharright_string[]="template_struct::right_one";staticconstexprcharwrong_string[]="template_struct::wrong_one";template