草庐IT

0Astatic_assert

全部标签

c++ - 索引上的 static_assert 在编译时知道

有没有办法静态断言编译时已知的索引,否则在运行时断言?示例:templateclassFoo{T_data[Dim];public:constT&operator[](intidx)const{static_assert(idxfoo;foo[0];foo[1];foo[2];//compilererrorfor(inti=0;i1}return0;} 最佳答案 您可以简单地抛出异常或断言。它将在constexpr上下文中编译失败。这仅在可以在constexpr上下文中评估抛出条件时才有效。请注意,某些版本的gcc中有一个错误会阻止

c++ - C++如何像Java Spring Assert一样检查条件并抛出异常

有没有标准的方法来做这样的事情?可用于Release模式(NDEBUG定义)检查失败时抛出异常。最好使用标准库或boost。为了清楚起见,我在这里使用的“断言”(可能是不同的术语)特别是关于运行时问题,而不是编程问题,例如SpringAssert。在Java世界中。Microsoft.VisualStudio.TestTools.CppUnitTestFramework是很好的候选人,但它是为了测试目的。 最佳答案 在我的一些项目中我使用:voidASSERT(constboolcond,conststd::string&text)

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