草庐IT

integral_constant

全部标签

c++ - cppcheck 认为我有 "Redundant code: Found a statement that begins with numeric constant"

Cppcheck(version1.46.1)对像这样的枚举发出以下警告:enumDATABASE_TYPE{DATABASE_TYPE_UNKNOWN=-1,//Redundantcode:Foundastatementthatbeginswithnumericconstant我不认为这是多余的。能够做那样的事情非常重要。这是cppcheck的错误还是我没有看到什么?更新我设法将它归结为一个最小的例子。这因为cppcheck有2个(更多)错误而变得复杂,这使得我的减少看起来没有效果。共有5个文件:a.h、a.cpp、b.h、b.cpp和inc。h包含以下内容。VC9在没有警告的情况下

c++ - std::regex_constants::optimize 使用的优化技术

我正在使用std::regex,在阅读std::regex_constants中定义的各种常量时,我​​遇到了std::optimize,阅读它,听起来它在我的应用程序中很有用(我只需要一个正则表达式实例,在开始时初始化,但在整个加载过程中多次使用它)。根据workingpapern3126(第1077页),std::regex_constants::optimize:Specifiesthattheregularexpressionengineshouldpaymoreattentiontothespeedwithwhichregularexpressionsarematched,a

c++ - clang 中 regex_constants 的错误实现?

如standard中所述:match_prev_avail:--first是一个有效的迭代器位置。设置后,会导致match_not_bol和match_not_bow被忽略但我运行以下代码并得到:#include#includeusingnamespacestd;intmain(){regexre0("^bcd");stringstr="abcd";std::string::iteratorstart=str.begin()+1;cout输出:010match_prev_avail似乎被match_not_bol覆盖了。 最佳答案

c++ - std::integral_constant 背后的原因是什么?

它的实际用例是什么?std::integral_constant我可以理解这是一个值为2的包装器:typedefstd::integral_constanttwo_t但为什么不直接使用2或用2定义一个constint值呢? 最佳答案 在少数情况下std::integral_constant非常有用。其中之一是标签分发。例如,std::true_type和std::false_type只是std::integral_constant和std::integral_constant分别。每个typetrait源自std::true_typ

c++ - 为什么使用静态 "constants"而不是实际值?

关闭。这个问题是opinion-based.它目前不接受答案。想要改进这个问题?更新问题,以便editingthispost可以用事实和引用来回答它.关闭5年前。Improvethisquestion我正在查看最小二乘法的代码,我遇到了以下几行:staticdoubleone=1.0;staticdoublep1=0.1;staticdoublep5=0.5;...我想知道为什么有人会为1.0定义一个static。例如,我可以理解pi有一些东西,但是对于1.0和0.1等微不足道的数学值?我认为这会降低代码的可读性,但它可能还有一些我遗漏的其他好处。那么,这些定义有原因吗?或者,如果它没

c++ - 什么是 "symbolic constants"和 "magic constants"?

在BjarneStroustrup的ATourofC++中,每章末尾都列出了一些建议。在第一章的结尾,其中一个写道:Avoid‘‘magicconstants;’’usesymbolicconstants;什么是魔法常量和符号常量? 最佳答案 somethingElse=something*1440;//amagicconstantsomethingElse=something*TWIPS_PER_INCH;//asymbolicone第一个是魔法常量的例子,它除了它的值之外没有传达任何其他信息。后者更有用,因为意图很明确。如果您有

c++ - 错误 : Expression must have integral or unscoped enum type

#include#include#include#include#includeusingnamespacestd;intmain(){floatsize;floatsumNum=0;floatmaxNum,minNum;floatmean;floattotalDev=0;floatdevSqr=0;floatstdDev;//Createauserinputsizestd::cout>size;float*temp=newfloat[size];//Gettinginputfromtheuserfor(intx=1;x>temp[x];}//Outputofthenumbersins

c++ - 为什么是非法的: copying vector of pointers into a vector of pointers to constants

问题以下代码无法在C++11(或C++14)中编译。我理解编译器的错误输出,但为什么标准不允许?//main.cpp#includeintmain(void){doublea=3.0;doubleb=3.0;//Itworkswithmerepointersconstdouble*ptrToConst=&a;/***/double*ptrToObj=&a;//ptrToObj=ptrToConst;//Illegal:that'sunderstandable…ptrToConst=ptrToObj;//Works//Butthesamedoesn'tworkwithvectorstop

c++ - 使用模板时得到 "cannot appear in a constant-expression"

templateclassCAT{};intmain(){inti=10;CATcat;return0;//hereIgoterror:‘i’cannotappearinaconstant-expression}甚至inti=10;constintj=i;CATcat;//thisstillcannotwork但我已经将i转换为constint,为什么编译器仍然报错?我的平台是ubuntu,gcc版本4.4.3谢谢,==============感谢大家的意见,但在某些情况下,我需要一个非常量变量,例如://alloperations.henumOPERATIONS{GETPAGE_FR

c++ - 当类型不是 Integral 时,如何使用 std::atomic 执行基本操作?

准确地说,我只需要将一个double增加另一个double并希望它是线程安全的。我不想为此使用互斥量,因为执行速度会急剧下降。 最佳答案 通常,C++标准库试图只提供可以高效实现的操作。对于std::atomic,这意味着可以在“通用”架构上的一两条指令中无锁地执行操作。“通用”架构具有针对整数的原子提取和添加指令,但不针对浮点类型。如果您想为原子浮点类型实现数学运算,您必须自己使用CAS(比较和交换)循环(LiveatColiru):std::atomicfoo{0};voidadd_to_foo(doublebar){autoc