草庐IT

c++ - 测试用例 VS ASSERTION 语句

在我的大多数C++项目中,我大量使用如下ASSERTION语句:intdoWonderfulThings(constint*fantasticData){ASSERT(fantasticData);if(!fantasticData)return-1;//,,,returnWOW_VALUE;}但是TDD社区似乎喜欢做这样的事情:intdoMoreWonderfulThings(constint*fantasticData){if(!fantasticData)returnERROR_VALUE;//...returnAHA_VALUE;}TEST(TDD_Enjoy){ASSERT_

c++ - 通过 decltype 表达式调用时 static_assert 是否应该工作?

我预计以下代码会因最后一行的static_assert检查而失败。但是在MSVC2015和gcc6.2中,它编译成功。它确实无法按预期在clang3.9中进行编译。这是编译器错误还是static_assert在decltype()中不起作用?#include#includetemplatestructWrapper{};templateconstexprstd::tupleoperator|(Wrapper,Wrapper){static_assert(std::is_same::value==false,"can'tcombinetwoofthesametype");returnst

c++ - 使用 boost spirit (longest_d) 解析 int 或 double

我正在寻找一种将字符串解析为int或double的方法,解析器应该尝试这两种选择并选择与输入流的最长部分匹配的那个。有一个已弃用的指令(longest_d)完全符合我的要求:number=longest_d[integer|real];...既然它已被弃用,还有其他选择吗?如果有必要实现语义操作来实现所需的行为,有人有什么建议吗? 最佳答案 首先,请切换到SpiritV2-多年来它已经取代了经典spirit。其次,您需要确保首选int。默认情况下,double可以很好地解析任何整数,因此您需要改用strict_real_polici

c++ - 如何告诉 static_assert constexpr 函数参数是 const?

我有一个看起来像这样的constexpr函数:constexprintfoo(intbar){static_assert(bar>arbitrary_number,"Usealowernumberplease");returnsomething_const;}但是,用GCC4.6.3编译这个一直告诉我错误:'bar'不能出现在常量表达式中我试过类似的东西constexprintfoo(constexprconstintbar){static_assert(bar>arbitrary_number,"Usealowernumberplease");returnsomething_cons

c++ - g++ 不编译带有断言的 constexpr 函数

templateconstexprinlineTgetClamped(constT&mValue,constT&mMin,constT&mMax){assert(mMinmMax?mMax:mValue);}error:bodyofconstexprfunction'constexprTgetClamped(constT&,constT&,constT&)[withT=longunsignedint]'notareturn-statement使用g++4.8.1。clang++3.4没有提示。谁在这里?有什么方法可以让g++在不使用宏的情况下编译代码? 最佳

c++ - 未定义的行为会影响 static_assert 吗?

考虑以下代码:SomeTypex=getX();for(automask=1u=sizeofx,"Typeofnumericparameteristoolong");/*...*/}此处,mask的类型为unsigned。假设SomeType是longlong。然后mask的初始化将由于移位太多而具有未定义的行为。但是OTOH,有一个static_assert,它检查未定义的行为不会在运行时发生(因为代码将无法编译)。但由于UB会导致时间悖论和其他意外情况,我不太确定static_assert能否保证在这种情况下实际工作。有什么理由可以确定这一点吗?或者是否应该重做此代码以使stati

c++ - constexpr std::array with static_assert

#include#includeintmain(intargc,char**argv){constexprconststd::arrayarr{{0,1}};constexprconstintarr2[]={0,1};static_assert(arr[0]==arr2[0],"asdf");static_assert(arr[1]==arr2[1],"asdfasdf");return0;}当使用gcc4.8.2和4.9.1使用g++test.cpp--std=c++11编译时,编译成功。但是,当使用clang++test.cpp--std=c++11使用clang3.4和3.5编译

c++ - 断言动态消息?

在我的程序中,我想使用显示错误消息的断言。除了众所周知的C和C++解决方案外,还有BOOST提供的“真正”解决方案BOOST_ASSERT_MSG(expr,msg)(另见assert()withmessage)但是静态消息对我来说还不够,有时我还想显示失败的变量,例如在这样的情况下BOOST_ASSERT_MSG(length>=0,"Nopositivelengthfound!Itis"如您所见,我想将消息“字符串”格式化为stringstream或ostream因为这样我就可以轻松显示自定义类型(假设我已经定义了相关的格式化函数)。这里的问题是BOOST_ASSERT_MSG默认

c++ - 在开关的默认标签中放置什么?

假设我有一个枚举。enumclassShapeName:char{TRIANGLE,CIRCLE,SQUARE};后来我有这样的功能:voidFunction(ShapeNameconstshape){switch(shape){caseShapeName::TRIANGLE:DoSomething1();break;caseShapeName::CIRCLE:DoSomething2();break;caseShapeName::SQUARE:DoSomething3();break;default://THISCODEBLOCKSHOULDNEVERBEEXECUTED!}retu

c++ - 断言中的 dynamic_cast 导致错误

我正在使用过时的VisualStudio2008(让我为您省去“这是您的问题”的麻烦。)这似乎是VisualStudio的问题:http://rextester.com/XKFR77690这似乎是assert的问题宏:http://ideone.com/bhxMi0给定这些结构:structbase{virtual~base(){}};templatestructFoo:base{Tfoo;};我能做到:base*test=newFoo>;if(dynamic_cast>*>(test)!=NULL)cout但是当我使用与if中完全相同的代码时-assert中的声明:assert(dy