草庐IT

assert_false

全部标签

c++ - getline() 如何在 while 循环条件中评估为 true 或 false?

例如while(getline(,))这种条件什么时候为真/假,为什么用它们代替其他情境条件? 最佳答案 whenisthiskindofconditiontrue/false鉴于documentationofstd::getline()它表示返回值是操作中涉及的流的std::istream引用,该流如何评估为true或假。这是使用重载的castoperatortobool完成的继承自std::ios类。此类定义指示当前流状态的状态标志,只有当流状态为good时才会评估true,其他状态如eof或fail将评估为false。andw

c++ - 是否可以编写这些 pure_assert 和 const_assert 宏?

GCC__attribute__((pure))和__attribute__((const))分别允许将函数声明为无副作用和引用透明;假设我想编写pure_assert和const_assert宏,其参数必须是适当严格级别的表达式,即:assert(oops_a_side_effect());静默导致调试和发布中的不同行为,但是:pure_assert(oops_a_side_effect());const_assert(oops_read_a_global());至少在调试版本中会出现编译时错误。由于我希望是显而易见的原因,您不能只创建一个声明为__attribute__((pure

c++ - 如何使用 BOOST_STATIC_ASSERT

#include#includeusingnamespacestd;//Iunderstandhowthefollowingtemplatefunctionworks//template//TGetMax(Ta,Tb){//Tresult;//result=(a>b)?a:b;//return(result);//}//Ihavedifficultiestounderstandhowthefollowingcodeworks//whenweshouldusethissyntaxtemplatevoidaccepts_values_between_1_and_10(){BOOST_STA

c++ - 支持逗号和错误信息的自定义 `assert`宏

我想创建一个自定义版本的assert中定义的宏,当断言失败时显示错误消息。所需的用法:custom_assert(AClass::aBoolMethod(),"aBoolMethodmustbetrue");有缺陷的测试实现:#definecustom_assert(mCondition,mMessage)...//ThisfailsbecausemConditionmayhavecommasinit#definecustom_assert(...,mMessage)//Notsureaboutthiseither-mMessagemaybeanexpressioncontaining

c++ - "warning C4800: ' int' : forcing value to bool 'true' or 'false' "不同场景下的不同行为

我无法理解此警告的以下行为。case1:boolread=(33&3);//NoWarningissuedbyvs2013case2:intb=33;boolread=(b&3);//NowcompilerisgeneratingC4800warning.为什么编译器在情况2中生成警告,而在情况1中不发出任何警告。 最佳答案 C4800是一个性能警告-在运行时将整数强制转换为bool会产生成本。这与逻辑正确性无关。最常见的强制转换(和警告)发生在您与使用整数(VC++中的BOOL)作为bool值的代码交互时。第一个代码段中的编译时强

c++ - 为什么我不能将 assert 与 std::is_same 一起使用?

有人可以向我解释为什么这个代码片段无法正常工作吗?#include#includeusingnamespacestd;intmain(){assert(is_same::value);}编译失败,因为根据编译器:prog.cpp:7:33:error:macro"assert"passed2arguments,buttakesjust1assert(is_same::value);^prog.cpp:Infunction'intmain()':prog.cpp:7:2:error:'assert'wasnotdeclaredinthisscopeassert(is_same::valu

c++ - 如何在Visual Studio中跳过Debug Assertion Failed直接break

我正在使用一些C++,我的代码中有一个拼写错误导致了这个问题。将来,我宁愿VisualStudio在直接从VisualStudio2017(社区版)以DEBUGMODE运行时直接中断错误代码,使用实际表达式,而不是向我显示这个烦人的提示。某处有这个设置吗? 最佳答案 Isthereasettingforthissomewhere?您可以通过settingreportmode以编程方式完成它:_CrtSetReportMode(_CRT_ASSERT,_CRTDBG_MODE_DEBUG);所需的header是.

c++ - 如何预期 static_assert 失败并使用 Boost.Test 框架处理它?

如果我有一个接受模板参数的方法,该模板参数应该可转换为base_of或与返回类型相同的类型,我应该怎么做?例如,考虑这个方法:templateclassIFoo{public:templateT*as(){static_assert(std::is_same::value||std::is_convertible::value||std::is_base_of::value,"IFoo::as()requiresServiceTtobeabaseofT");...}};现在,我想对其进行BOOST_CHECK!classA{};classB{};BOOST_AUTO_TEST_CASE

c++ - 当我的项目在 Visual Studio 和 Qt Creator 中构建时,Q_ASSERT 具有不同的行为

这似乎是一个错误,因为在QtCreator上,Q_ASSERT(false)导致我的应用程序退出,即使文档说它应该允许您中断断言:OnWindows,fordebugbuilds,thisfunctionwillreporta_CRT_ERRORenablingyoutoconnectadebuggertotheapplication.在QtCreator上:当在QtCreator+MinGW中编译我的项目时,代码Q_ASSERT(false);导致程序显示此消息(下面的屏幕截图),之后应用程序退出:ThisapplicationhasrequestedtheRuntimetoterm

c++ - 将 Boost 序列化与 xml_oarchive 一起使用时 assertion_failed

在编译BoostSerialization的简单测试时:classTest{protected:intNum;friendclassboost::serialization::access;templatevoidserialize(Archive&ar,constunsignedintversion){ar&BOOST_SERIALIZATION_NVP(Num);}public:Test():Num(0){}~Test(){}};使用xml_oarchive进行输出,我遇到以下GCC错误:C:\Development\Libraries\boost_1_55_0\boost\mpl