草庐IT

remove_copy_if

全部标签

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

c++ - 杂乱的函数指针 : how to remove the warning?

正如我在thispost中提问和回答的那样.我有以下示例代码。#includecharfoo(){return'a';}charbar(){return'b';}charblurga(){return'c';}charbletch(){return'd';}char(*gfunclist[])()={foo,bar,blurga,bletch};char(*(*x())[])(){staticchar(*funclist[4])()={foo,bar,blurga,bletch};returnfunclist;}intmain(){printf("%c\n",gfunclist[0](

c++ - 丢失的一元 std::copy 的最佳实现

C++11引入了语义以避免不必要的对象复制,std::move引入了语义,否则会发生复制。但是,现在也有一些情况需要拷贝,但默认情况下不需要。例如,考虑一下reverse的这种简单实现。因为基于范围的for使用完美转发,所以在循环内修改容器相当于损坏。autoout_iter=container.rbegin();for(autovalue:container){*out_iter++=value;}目标是使用解决这个问题for(autovalue:copy(container)){这看起来很简单……接受任何参数,获取底层类型并返回一个临时拷贝。 最佳答案

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++ - 如何写多个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++中没有逻辑异或,但您可以针对