草庐IT

if-constexpr

全部标签

c++ - "constexpr if"比 switch 语句好吗?

C++17引入了根据编译时条件实例化的“constexprif”。这是否意味着在模板函数中使用“constexprif”比使用switch语句更好?例如:templatevoidfunc(){ifconstexpr(val==0){}elseifconstexpr(val==1){}else...ifconstexpr(val==k){}else{}}//vstemplatevoidfunc(){switch(val){case0:break;case1:break;...casek:break;default:break;}} 最佳答案

c++ - 如何在 constexpr string_view 上使用 std::string_view::remove_prefix()

std::string_view::remove_prefix()和std::string_view::remove_suffix()都是c中的constexpr成员函数++17;但是,它们会修改调用它们的变量。如果值是constexpr,它也将是const并且不能修改,那么这些函数如何用于constexpr值?换句话说:constexprstd::string_viewa="asdf";a.remove_prefix(2);//compileerror-aisconst如何在constexprstd::string_view上使用这些函数?如果它们不能在constexprstd::s

c++ - 使用 "if constexpr"和 SFINAE 禁用分支

我想在编译时启用/禁用分支,这取决于是否可以使用某些参数调用函数。ifconstexpr条件必须包含什么?我可以通过std::result_of(decltype(add)(A,B))获取结果类型,但是如何检查结果类型是否有效?(即如何将此信息转换为bool?)constautoadd=[](constautoa,constautob){returna+b;};constautosubtract=[](constautoa,constautob){returna-b;};templatevoidfoo(Aa,Bb){ifconstexpr(/*canadd(a,b)becalled?*

c++ - 复制初始化: why move or copy constructor was not called even if copy-elision is turned off?

我的问题不同,因为我可能“知道”复制省略。我正在学习复制初始化。但是,以下代码让我感到困惑,因为我已经使用-fno-elide-contructors-O0选项关闭了复制省略。#includeusingnamespacestd;classtest{public:test(inta_,intb_):a{a_},b{b_}{}test(consttest&other){cout我首先使用命令构建:g++-std=c++11-fno-elide-constructors-O0main.cpp-omain得到如下结果:**showelideconstructors**moveconstruct

c++ - if constexpr 和 C4702(以及 C4100 和 C4715)

有没有办法解决以下问题:此代码生成C4702警告“无法访问的代码”(在带有/std:c++17的VC++15.8上)templateinlineboolMatchMonostate(VariantType&variant){SUPPRESS_C4100(variant);ifconstexpr(std::is_same_v){variant=std::monostate();returntrue;}returnfalse;//!!!unreachableiftheaboveistrue!!!=>C4702}为了抑制C4100的“未引用形式参数”警告,我已经在使用技巧了#defineSU

c++ - 在包含 shared_ptr 的 map 上使用 find_if 会增加引用计数

我正在创建一个程序,它有一个包含shared_ptr的映射。当我尝试使用std::find_if在其中查找元素时,shared_ptr的引用计数会增加。示例:#include#include#include#includeintmain(void){std::map>map;map[1]=std::make_shared(3);map[2]=std::make_shared(5);map[4]=std::make_shared(-2);autoit=std::find_if(map.begin(),map.end(),[](conststd::pair>&elem){std::cout

现在允许对ConstexPR静态数据成员的重新定义? (但不是Inline Const)?

以下未能在C++14中的GCC和Clang下进行编译,但C++1Z成功:structCls{staticconstexprintN=0;};constexprintCls::N;constexprintCls::N;C++14错误是可以预见的:redefinitionof‘constexprconstintCls::N’是什么改变了这一合法?我发现:N465910.1.5[DCL.Constexpr]使用ConstexPR规范声明的函数或静态数据成员隐含是内联函数或变量所以我认为这可能与内联变量有关,但是两个编译器下的C++1Z失败structCls{staticinlineconstintN

php - 多个 if() 合而为一

关闭。这个问题是off-topic.它目前不接受答案。想改进这个问题吗?Updatethequestion所以它是on-topic用于堆栈溢出。关闭10年前。Improvethisquestion这个:if(A||B){X;}if(C&&D){X;}if(F!=G&&H+I===J){X;}可以替换为:if((A||B)||(C&&D)||(F!=G&&H+I===J)){X;}但它是否可以被替换为:if(A||B||C&&D||F!=G&&H+I===J){X;}?(没有括号)语言之间有什么区别吗?P.S:答案不应基于这些示例。

c++ - 用于编译时强制 constexpr 函数评估的单个表达式助手可能吗?

@cyberpunk_正在努力实现某些目标并提出一些问题,但所有的追求都归结为:是否可以构建一个工具来强制执行constexpr函数的编译时评估?intf(inti){returni;}constexprintg(inti){returni;}intmain(){f(at_compilation(g,0));intx=at_compilation(g,1);constexprinty=at_compilation(g,2);}在所有情况下,at_compilation强制执行g的编译时评估。at_compilation不需要采用这种形式。要求允许任何(原生数字)文字类型作为conste

c++ - 如何写多个if条件

我有两个变量A和B我想写一段代码,如果两个变量之一等于151or156or720并且另一个不等于其中一个数字,则第三个变量C=0等于1。例如1)ifA=151andB=700thenC=12)ifA=151andB=720thenC=03)ifA=140andB=700thenC=0这是代码intA=0cin>>A;intB=0cin>>B;intC=0;intDECKlist[3]={151,156,720}for(intd=0;d这样可以吗?还有其他更好的方法吗? 最佳答案 这是一个异或,异或。C++中没有逻辑异或,但您可以针对