jjzjj

c++ - 引用模板参数类型的 static_assert

我正在尝试做的是这个简单的模板钳制功能。我想确保upper>=lower在运行时和编译时。templateTclamp(constT&lower,constT&upper,constT&n){weak_assert(upper>=lower);returnstd::max(lower,std::min(n,upper));}这样写似乎合理:static_assert(upper>=lower,"invalidbounds");但是,当使用非constexpr调用时参数,编译器给我这个:Static_assertexpressionisnotanintegralconstantexpre

c++ - 对模板中的所有其他类型执行 static_assert

如何对模板中的所有其他类型执行static_assert(或其他检查)?template//T1,T2,T3,...structfoo{//HowcanI//forT1,T3,T5,T7,...//dosomechecks,forexample://static_assert(std::is_default_constructible::value,"invalidtype");//static_assert(std::is_copy_constructible::value,"invalidtype");}; 最佳答案 请试试这个

c++ - Boost序列化断言失败

我使用boost的二进制序列化,直到现在它运行良好。我有std::list指向序列化输出(oarchive)的指针,但序列化在对象的serialize()函数中失败,带有MSVC的对话框:R6010-abort()hasbeencalled这样的字符串被打印到控制台窗口中:Assertionfailed:0==static_cast(t)||1==static_cast(t),filec:\programfiles\boost\boost_1_44\boost\archive\basic_binary_oprimitive.hpp,line91这是什么意思?项目非常大,资源是分布式的,

c++ - 如何仅在实际使用成员模板时才在成员模板中进行 static_assert?

考虑这个简单的类:templateclassFoo{public:Foo(Tconst&val):_val(val){}templateFoo(Fooconst&){static_assert(false,"CannotconvertfromFootoFoo.");}operatorT&(){return_val;}operatorTconst&()const{return_val;}private:T_val;};它允许从模板类型隐式构造并隐式转换回该类型,一个简单的包装器。现在,我不想启用不相关的Foo之间的转换,由于这些隐式构造/转换,这是可能的。我可以将模板化复制构造函数设为私

c++ - 断言(3/2 == 1): Does this work?

我刚刚了解到除法运算符的舍入行为在C++11之前没有定义。解决方案是使用std::div。(Safelyroundtonextsmallermultiple)我的程序总是假定/只会截断小数部分。作为快速修复,我想包含一个断言,这样如果有人在具有不同舍入行为的平台上进行编译,我至少会得到一个错误。assert(3/2==1)或static_assert(3/2==1)会完成这项工作吗?或者这些常量是否会被编译器内部的算法优化掉,这可能与机器实际做的不同? 最佳答案 “我刚刚了解到除法运算符的舍入行为在C++11之前没有定义”。如果两个

c++ - 带 static_assert() 的逗号运算符

当尝试使用static_assert作为参数来计算逗号运算符时编译失败voidfvoid(){}intmain(){inta=(1,2);//a=2intb=(fvoid(),3);//b=3intd=(,5);//^//error:expectedprimary-expressionbefore','token.OKintc=(static_assert(true),4);//^~~~~~~~~~~~~//error:expectedprimary-expressionbefore'static_assert'.Why?}看起来static_assert()在编译后甚至没有解析为vo

c++11:为什么 std::forward 中的 static_assert 是必需的?

在move.h中,forward有两个重载templateconstexpr_Tp&&forward(typenamestd::remove_reference::type&__t)noexcept{returnstatic_cast(__t);}templateconstexpr_Tp&&forward(typenamestd::remove_reference::type&&__t)noexcept{static_assert(!std::is_lvalue_reference::value,"templateargumentsubstituting_Tpisanlvalueref

c++ - 使用 static_assert 确定特定模板参数是否为特定无类型类模板

我想要一个将参数限制为仅派生自特定模板类的类型的函数。在这种情况下,basic_string(来自STL-docs)。例如,声明了一个wstring:typedefbasic_string,allocator>wstring;基本思路是这样的:templatevoidstrings_only_please(TStringmessage){static_assert(is_base_of::value,"Notastringtype!");}当然,虽然没有指定basic_string,但它无法编译……它需要一个真实的类型。(虽然我可能只对少数实际字符串类型进行硬编码,但我正在寻找针对此模

c++ - Assert错误,用cout证明

当我运行它时,在main()中,cout打印5.395。但是断言说它失败了!!这真是令人难以置信,为什么会这样?#include#includeusingnamespacestd;constfloatA=1.6;constfloatC=1.55;constfloatG=2.2;constfloatT=1.23;charempty[18];intarraySize;voidcopyArray(charsourceArray[],chartargetArray[],intsize){for(inti=0;i编辑:感谢所有的回答,我只是乘以1000,转换为int,转换回double,然后除以

c++ - 有序非重入调用的简单断言?

我有两个功能:将按顺序调用的voidprepare()和voidfinish()如下:prepare();;finish();...prepare();;finish();我想做一个简单的断言来简单地测试它们实际上是以这种方式被调用的,并且它们没有在应用程序中并发或乱序调用。此应用程序是单线程应用程序。这是一个简单的开发/测试健全性检查,以确保按顺序调用这些函数,并且无论出于何种原因,它们都不会被同时调用。此外,由于性能至关重要,因此生产代码中应省略这些断言/健全性检查!像这样的简单assert()是否效果最好?inttest=0;voidprepare(){assert(++test