草庐IT

bool_constant

全部标签

c++ - 警告 C4800 : 'int' : forcing value to bool 'true' or 'false' (performance warning)

我的代码中有这个问题:boolCBase::isNumber(){return(id&MID_NUMBER);}boolCBase::isVar(){return(id&MID_VARIABLE);}boolCBase::isSymbol(){return(id&MID_SYMBOL);} 最佳答案 仅供引用:强制转换不会隐藏警告bydesign.类似return(id&MID_NUMBER)!=0;应该明确说明“我要检查这个值是否为零”并让编译器高兴 关于c++-警告C4800:'in

c++ - 警告 : ISO C++ forbids converting a string constant to ‘char*’ for a static `constexpr char*` data member

这个问题在这里已经有了答案:constexprconstvsconstexprvariables?(3个回答)关闭3年前。为什么这段代码会返回警告warning:ISOC++forbidsconvertingastringconstantto‘char*’[-Wwrite-strings]如果Aconstexprspecifierusedinanobjectdeclarationornon-staticmemberfunction(untilC++14)impliesconst.Aconstexprspecifierusedinafunctionorstaticmembervariab

c++ - 为什么 c++ 中 char 和 bool 的大小相同?

我正在阅读C++编程语言。Stroustrup在其中指出sizeof(char)==1和1.具体取决于实现。为什么像boolean值这样简单的值会占用与字符相同的空间? 最佳答案 在现代计算机体系结构中,字节是最小的可寻址内存单元。要将多个位打包到一个字节中,需要应用额外的位移操作。在编译器级别,这是内存与速度要求之间的权衡(在高性能软件中,那些额外的位移操作会不必要地增加和减慢应用程序的速度)。 关于c++-为什么c++中char和bool的大小相同?,我们在StackOverflow

c++ - __STDC_LIMIT_MACROS 和 __STDC_CONSTANT_MACROS 是什么意思?

我在系统的标准C++库以及我正在使用的库中的一些头文件中看到了这一点。这两个定义的语义是什么?除了源本身之外,还有像这样的#defines的好的引用吗? 最佳答案 __STDC_LIMIT_MACROS和__STDC_CONSTANT_MACROS是一种解决方法,允许C++程序使用C99标准中指定但不在C++标准。UINT8_MAX、INT64_MIN和INT32_C()等宏可能已经在C++应用程序中以其他方式定义。为了让用户决定是否要像C99那样定义宏,许多实现要求在stdint.h之前定义__STDC_LIMIT_MACROS和

c++ - std::bitset 如何比 std::vector<bool> 更快?

根据thisanswer发帖人需要std::bitset100k位的大小比std::vector快查询单个位时。这怎么可能?如果std::bitset的话,它们在实现上怎么可能有显着差异?显然允许任意大小,就像std::vector? 最佳答案 VisualStudio2010的测量结果表明std::bitset不通常比std::vector快.确切的原因是什么我不能说-只是bitset的实现与std::vector完全特化有很大不同。std::bitset通过a将其全部内容存储在对象中templateclassbitset....

c++ - Range-for-loops 和 std::vector<bool>

为什么这段代码有效std::vectorintVector(10);for(auto&i:intVector)std::cout这不是吗?std::vectorboolVector(10);for(auto&i:boolVector)std::cout在后一种情况下,我得到一个错误error:invalidinitializationofnon-constreferenceoftype‘std::_Bit_reference&’fromanrvalueoftype‘std::_Bit_iterator::reference{akastd::_Bit_reference}’for(aut

c++ - 为什么 std::atomic<bool> 比 volatile bool 慢得多?

多年来我一直在使用volatilebool进行线程执行控制,效果很好//inmyclassdeclarationvolatileboolstop_;-----------------//Inthethreadfunctionwhile(!stop_){do_things();}现在,由于C++11增加了对原子操作的支持,我决定尝试一下//inmyclassdeclarationstd::atomicstop_;-----------------//Inthethreadfunctionwhile(!stop_){do_things();}但它比volatilebool慢几个数量级!我编

c++ - 为什么标准本身没有将 sizeof(bool) 定义为 1?

char、signedchar和unsignedchar的大小由C++标准本身定义为1个字节。我想知道为什么它也没有定义sizeof(bool)?C++03标准$5.3.3/1说,sizeof(char),sizeof(signedchar)andsizeof(unsignedchar)are1;theresultofsizeofappliedtoanyotherfundamentaltype(3.9.1)isimplementation-defined.[Note:inparticular,sizeof(bool)andsizeof(wchar_t)areimplementation

c++ - 我什么时候会在 constexpr 上使用 std::integral_constant?

#include#includeintmain(){//creatinganintegralconstantwithconstexprconstexprunsignedintspeed_of_light{299792458};//creatinganintegralconstantwithstd::integral_constanttypedefstd::integral_constantspeed_of_light_2;//usingthemstd::coutstd::integral_constant有什么特别之处,我会选择使用它而不是constexpr?他们的行为和用例看起来和我

c++ - Consexpr if 具有非 bool 条件

我似乎发现了Clang和GCC不同意的地方。代码如下:intmain(){ifconstexpr(2){}}使用GCC7.4.0编译成功,但使用Clang7.0.0编译失败,并显示以下错误消息:test.cpp:3:17:error:constexprifconditionevaluatesto2,whichcannotbenarrowedtotype'bool'[-Wc++11-narrowing]ifconstexpr(2){}^1errorgenerated.cppreference似乎没有提到“缩小”,所以这似乎是一个Clang错误,但我并不完全确定。如果这是任一编译器的错误,