草庐IT

readability-implicit-bool-convers

全部标签

带有 C++ bool 函数的 C# DllImport 未正确返回

我在C++DLL中有以下函数extern"C"__declspec(dllexport)boolExist(constchar*name){//if(g_Queues.find(name)!=g_Queues.end())//returntrue;//else//returnfalse;returnfalse;}在我的C#类中,我有以下内容:[DllImport("Whisper.dll",EntryPoint="Exist",CallingConvention=CallingConvention.Cdecl)]publicstaticexternboolExist(stringnam

c++ - "Implicit instantiation of undefined template"前向声明模板类时

我有一些代码需要在其中前向声明一个模板类(或者至少,前向声明对我来说会让事情变得更容易......)。我已经编写了我遇到的问题的简化版本,所以我可以在这里显示它:templateclassMyTemplateClass;intmain(intargc,char*argv[]){MyTemplateClassmyTemp;//errorheremyTemp.GetTheValue();return0;}templateclassMyTemplateClass{intm_myint;floatm_myfloat;public:MyTemplateClass():m_myint(5),m_m

c++ - vector<bool> 和数组之间的性能差距

我试图解决acodingprobleminC++计算小于非负数的素数个数n.所以我首先想出了一些代码:intcountPrimes(intn){vectorflag(n+1,1);for(inti=2;i这需要88毫秒并使用8.6MB内存。然后我把我的代码改成:intcountPrimes(intn){//vectorflag(n+1,1);boolflag[n+1];fill(flag,flag+n+1,true);for(inti=2;i这需要28毫秒和9.9MB。我真的不明白为什么在运行时间和内存消耗上都有这样的性能差距。我已阅读相关问题,例如thisone和thatone但我还

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++ - 为什么 c++ 中 char 和 bool 的大小相同?

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

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++ - 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错误,但我并不完全确定。如果这是任一编译器的错误,