jjzjj

assertions

全部标签

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++ - 为什么我不能将 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

c++ - 如何 static_assert 给定的函数调用表达式是否可以编译?

有没有一种方法可以检查函数调用表达式是否会在编译时进行编译并对其进行static_assert?还是我应该通过system()调用编译器并检查退出代码?#includetemplatevoidf(Args&&...args,bool);templatevoidg(Args&&...args);intmain(){g(1,2.0,"hello",false);//compilesf(1,2.0,"hello",false);//doesn'tcompile//HowdoIdothis?//static_assert(!does_this_compile(f(1,2.0,"hello",f

C++ 可变参数模板特化(和 static_assert)

是否可以专门化此模板声明:templateTYPEFoo(ARGS...args){static_assert(false);}我尝试了一些事情,例如:templateintFoo(floatargs){return42;}...但是当我尝试这样使用它时,我总是会遇到静态断言:autovalue=Foo(1.5f);正确的语法是什么? 最佳答案 您不得编写仅在未实例化时才有效的模板。这与标准中的以下规则相冲突:Ifnovalidspecializationcanbegeneratedforatemplate,andthattempl