草庐IT

C_ASSERT

全部标签

c++ - 是否有满足 C99 标准的 static_assert 替代品?

我一直在尝试实现类似于C++11标准中定义的static_assert的方法。主要问题是C++编译器如何将传递给static_assert的文本消息编写为constchar*?我可以让编译器编写类似A_is_not_POD的消息。这就是我所拥有的:#defineMY_STATIC_ASSERT(condition,name)\typedefcharname[(condition)?1:-1];但是让编译器编写类似"Error:AisnotPOD."之类的东西会非常好。有什么建议吗? 最佳答案 不确定我是否理解问题,但C11有_Sta

c++ - BOOST_MPL_ASSERT 和 BOOST_STATIC_ASSERT 哪个更好?

我记得BOOST_MPL_ASSERT曾经是首选。这仍然是真的吗?有人知道为什么吗? 最佳答案 [回答我自己的问题]这取决于。这是一个苹果与橘子的比较。尽管相似,但这些宏不可互换。以下是每个工作原理的摘要:BOOST_STATIC_ASSERT(P)如果P!=true则生成编译错误.BOOST_MPL_ASSERT((P))如果P::type::value!=true则生成编译错误.尽管需要双括号,后一种形式特别有用,因为它可以生成更多信息性错误消息如果使用bool空元元函数来自Boost.MPL或TR1的作为谓词。这是一个示例程序

assert.h 中的 C++ 断言实现

00001/*assert.h00002Copyright(C)2001,2003FreeSoftwareFoundation,Inc.00003WrittenbyStephaneCarrez(stcarrez@nerim.fr)0000400005Thisfileisfreesoftware;youcanredistributeitand/ormodifyit00006underthetermsoftheGNUGeneralPublicLicenseaspublishedbythe00007FreeSoftwareFoundation;eitherversion2,or(atyour

c++ - 内联函数上的 static_assert 给出错误

考虑以下情况typedefvoid(*foo)();templatestructbar{static_assert(f!=nullptr,"f==null!");};voidbaz(){}inlinevoidbax(){}barok;barbad;//error:non-constantconditionforstaticassertionbaz和bax都被接受为模板参数。它表明两者都被接受为常数。然而,在static_assert他们似乎是不同的(至少在gcc4.9中)-bax不再是一个常数。我的假设是static_assert和模板评估常数相同。例如。任何一个错误都应该是'bax不

c++ - constexpr、static_assert 和内联

我之前询问过functionoverloadingbasedonwhethertheargumentsareconstexpr.我正在尝试解决该问题的令人失望的答案,以制作更智能的断言功能。这大致就是我想要做的:inlinevoidsmart_assert(boolcondition){if(is_constexpr(condition))static_assert(condition,"Error!!!");elseassert(condition);}基本上,如果可以在编译时进行检查,那么编译时检查总是比运行时检查要好。但是,由于内联和常量折叠之类的原因,我不能总是知道是否可以进行

c++ - static_assert 内部/外部类定义

为什么static_assert需要在类定义之外?失败代码#includeclassA{public:A(A&&)noexcept{}static_assert(std::is_nothrow_move_constructible::value,"ERROR");};intmain(){}工作代码#includeclassA{public:A(A&&)noexcept{}};static_assert(std::is_nothrow_move_constructible::value,"ERROR");intmain(){}什么时候适合在类或结构的定义中使用static_asserts

c++ - 编译时等效的 std::cout,或 c++11 中编译时常量值的 static_assert 字符串化

有没有办法打印constexpr的值?或#defined编译时的值?我想要std::cout的等价物,或某种方式来做类似的事情constexprintPI_INT=4;static_assert(PI_INT==3,const_str_join("PI_INTmustbe3,not",const_int_to_str(PI_INT)));编辑:我可以使用constexpr进行一些基本的编译时打印s,至少在gcc上做类似的事情templatestructdisplay_non_zero_int_value;templatestructdisplay_non_zero_int_value{

c++ - MSVC++ : template's static_assert is not triggered inside a lambda

更新2:这已在VS2019Preview16.1Preview1中得到修复。更新:我已在visualstudio.com提交错误报告.所以我开始研究C++的模板,当我试图阻止使用static_assert编译模板类时遇到了这个问题。基本上,static_assert错误在VS2017上使用C++语言标准:ISOC++17标准(/std:c++17)。我也在gcc-7上使用-std=c++17进行了尝试,并触发了错误。这是VS2017上的错误还是我遗漏了什么?代码示例:#include#include#includetemplateclassIntegralContainer{stati

C++ 断言 : the precedence of the expression in an assert macro

在C++中:assert(std::is_same::value);//doesnotcompileassert((std::is_same::value));//compiles谁能解释一下原因? 最佳答案 assert是一个预处理器宏。预处理器宏是愚蠢的;他们不懂模板。预处理器在括号内看到10个标记:assert(std::is_same::value);它以逗号分隔。它不知道这是错误的分割位置,因为它不明白std::is_same和int>::value不是有效的C++表达式。预处理器足够聪明,不会在多个参数之间分解内部括号对

c++ - 我误解了 assert() 的用法吗?

我在看theassert()referencepage当我阅读给定的示例时,我被卡住了:/*assertexample*/#include#includeintmain(){FILE*datafile;datafile=fopen("file.dat","r");assert(datafile);fclose(datafile);return0;}Inthisexample,assertisusedtoaborttheprogramexecutionifdatafilecomparesequalto0,whichhappenswhenthepreviouscalltofopenwasn