草庐IT

if-constexpr

全部标签

c++ - constexpr 结构是否被零初始化?

考虑以下示例:templatestructfoo{constexprfoo():a(){}inta[N];};intmain(){foo{}).a[0]>f;}尝试编译时,clang推断出foo作为f的类型同时g++因内部编译器错误而崩溃。然而,是a-foo的成员|保证为零,还是这种未定义/未指定的行为? 最佳答案 成员初始化器a()值初始化foo::a(通过[class.base.init]/7,这导致[dcl.init]/11)。[dcl.init]/8指定数组的值初始化对数组的每个元素进行值初始化。对于ints(和其他基本类型

c++ - 传递 constexpr 对象

我决定尝试一下constexpr的新C++14定义,为了充分利用它,我决定编写一些编译时字符串解析器。但是,在将对象传递给函数时,我一直在努力保持对象为constexpr。考虑以下代码:#include#includeclassstr_const{constchar*constp_;conststd::size_tsz_;public:templateconstexprstr_const(constchar(&a)[N]):p_(a),sz_(N-1){}constexprcharoperator[](std::size_tn)const{returnn我使用str_constclas

c++ - 打开 : check if nested parallesim

假设我有一个方法将两个std::vector相乘:doublemultiply(std::vectorconst&a,std::vectorconst&b){doubletmp(0);/*hereIcouldeasilydoaparallelizationwith*//*#pragmaompparallelloopfor*/for(unsignedinti=0;i如果我在此函数中设置pragma宏,将运行对multiply(...)的调用在所有线程上。现在假设我想在其他地方做很多vector乘法:voidmany_multiplication(std::vector*a,std::ve

c++ - 在 C++14 中,constexpr 成员可以更改数据成员吗?

在C++14中,由于constexpr不再是隐式const,constexpr成员函数是否可以修改类的数据成员:structmyclass{intmember;constexprmyclass(intinput):member(input){}constexprvoidf(){member=42;}//Isitallowed?}; 最佳答案 是的,我相信这个变化是从proposalN3598:constexprmemberfunctionsandimplicitconst开始的并最终成为N3652:Relaxingconstrain

c++ - 避免在静态 bool 值上使用 if 语句进行逻辑决策

我有一个类,其成员itemType仅设置一次且从未修改过,但在许多if语句中使用它来决定调用哪个函数。由于itemType仅设置一次,因此有办法避免类中其他地方的if语句。这将简化和清理代码,并且作为奖励还将节省if检查的开销。我正在考虑一个指针函数,我可以根据itemType值在构造函数中初始化它。有没有更好的替代方法?请注意原始类和代码库很大,我无法根据项目类型创建子类。enumItemTypes{ItemTypeA,ItemTypeB,};classItemProcessing{public://ThisfunctioniscalledhundredsoftimesvoidPro

C++ - 为什么程序在映射迭代器中使用 if 语句崩溃?

我是C++的新手,我试图在传递if语句的同时遍历映射。但是程序崩溃了。请帮我修复程序。#include#include#include#include#includeusingnamespacestd;intmain(){std::maph;std::map::iteratorit;h[1]=2;h[4]=5;for(it=h.begin();it!=h.end();it++){if(it->second>4){h.erase(it->first);}} 最佳答案 您正在删除for循环内的元素,指向已删除元素(即it)的迭代器将失效

c++ - constexpr 递归函数是否使用 if constexpr

使用gcc(HEAD7.0.0201612)我惊讶地发现这有效:constexprlongvalue(constchar*definition){if(definition&&*definition){return*definition+value(definition+1);}return*definition;}intmain(){longl{};std::cin>>l;switch(l){casevalue("AAAA"):f1();break;casevalue("BBBB"):f2();break;default:error();break;}return0;}文字字符串"A

c++ - 将 constexpr 结构转换为运行时结构

我正在尝试使用模板类(此处为Foo),其基本类型如下:hana::tuple,Runtime>>与Runtime一个显然不能是constepxr的类.但是类型可以用多种方式构造,这就是我使用的原因:hana::tuple,hana::type>>在编译时完成工作。所以问题基本上是如何从第一个元组类型转换为第二个元组类型。不知hana里面有没有东西那可以帮助我。或者更好的是,关于这种“转换”的一些技巧。namespacehana=boost::hana;usingnamespacehana::literals;structRuntime{std::vectordata;};templat

c++ - enable_if 检查迭代器的值类型是否是一对

我想为值类型为一对的迭代器编写一个专门的模板函数。我的期望是这应该匹配std::map的迭代器。为了检测对:templatestructis_pair:std::false_type{};templatestructis_pair>:std::true_type{};//alsotriedthis,butitdidn'thelptemplatestructis_pair>:std::true_type{};然后我在函数声明中使用enable_if:templatedecltype(auto)do_stuff(std::enable_if::value,ITR>itr){//access

c++ - 使用 "if constexpr"防止元组越界

以下代码在GCC和Clang下编译良好,但在VisualStudio(/std:c++latest)的最新更新中停止工作:#includetemplatevoidcheck_tuple(T...types){ifconstexpr(pos>::type;}}intmain(){check_tuple(1.0,1.0);check_tuple(1.0,1.0);}在最新版本的VisualStudio(/std:c++latest)中,编译失败,元组索引越界(std::tuple_element>)。是否可以像这样使用constexpr来防止元组越界? 最佳答案