草庐IT

disable_if

全部标签

c++ - constexpr if 和 static_assert

P0292R1constexprif一直included,在C++17的轨道上。它似乎很有用(并且可以替代SFINAE的使用),但是关于static_assert的评论是错误的,不需要诊断在false分支中吓到我了:Disarmingstatic_assertdeclarationsinthenon-takenbranchofaconstexprifisnotproposed.voidf(){ifconstexpr(false)static_assert(false);//ill-formed}templatevoidg(){ifconstexpr(false)static_asser

c++ - std::remove_if 和 erase 不从 std::vector 中移除元素

我正在练习leetcodeeasy问题。我想使用lambda从vector中删除_if(这是第一次,太棒了)。我得到一个指向new_end的负指针。#include#include#include#include//std::greaterusingnamespacestd;intmain(){vectora={2,7,11,15};inttarget=9;autonew_end=std::remove_if(a.begin(),a.end(),[&a,target](constintx){returnstd::count(a.begin(),a.end(),x)>target;});

c++ - C++17 中的 "If constexpr"在非模板函数中不起作用

我尝试使用C++17标准。我尝试使用C++17ifconstexpr的功能之一。我有一个问题......请看下面的代码。这编译没有错误。在下面的代码中,我尝试使用ifconstexpr来检查它是否是一个指针。#include#includetemplatevoidprint(Tvalue){ifconstexpr(std::is_pointer_v)std::cout但是当我重写上面的代码时,如下所示,其中ifconstexpr在main函数中:#include#includeintmain(){autovalue=100;ifconstexpr(std::is_pointer_v)s

在if语句中使用字符串

我需要一种方法让用户输入在if陈述:print("Hello,World!")name=input("Whatisyourname?")hobby=input("Cool,so"+name+"whatdoyoudoyouforfun?Youcansaysomethinglikeplay,work,learn,etc.")play='play'work='work'learn='learn'ifhobby=playprint('awesome')elifhobby=workprint('mustbebusy')elifhobby=learnprint('ha,metoo')看答案这if,elif

c++ - 对于 CLang 中的 enable_if 错误(错误 11723)是否有更好的解决方法?

理想情况下,我们可以使用enable_if做类似的事情:#includenamespacedetail{enumclassenabler_t{DUMMY};}templateusingenable_if_u=typenamestd::enable_if::type;templateusingdisable_if_u=typenamestd::enable_if::type;template::value>...>inta(){return0;}template::value>...>doublea(){return0.0;}intmain(){autox=a();}恕我直言,这是最好的

c++ - VS2013 : How to disable warnings for included header files outside of the project

在我的项目中,我包含了一个由外部库提供的头文件。使用/W3,所有内容都可以在没有警告的情况下编译。但是,我希望我的项目能够使用/W4进行干净地编译。这对我的代码来说没有问题,但外部header会发出大量警告。我知道我可以做这样的事情:#pragmawarning(push)#pragmawarning(disable:####)//includehere#pragmawarning(pop)但是有一长串要禁用的警告。有没有一种方法可以在包含此header时将警告级别设置回/W3,同时仍使用/W4编译我的其余代码?谢谢! 最佳答案 #

c++ - 简单 if 是否会降低性能?

比如我有一个类classPoint{public:floatoperator[](inti)const{if(i==0)returnm_x;//simpleifs,performancereduction??if(i==1)returnm_y;returnm_z;}private:floatm_x;floatm_y;floatm_z;};与访问std::array的元素相比,性能是否有任何降低??如果是这样,我该如何删除它。我想使用数组以外的字段x、y、z。 最佳答案 Isthereanyperformancereduction?我

c++ - if 语句不起作用并被跳到 else 部分

//thisismysourcefile,.cpp#include#include#include"kingdom.h"namespacewesteros{voiddisplay(KingdompKingdom[],intkingdomElement,stringKingdomName){cout#include"kingdom.h"#includeusingnamespacestd;usingnamespacewesteros;intmain(void){intcount=0;Kingdom*pKingdoms=nullptr;pKingdoms=newKingdom[count];

C++:一种在分隔变量定义和测试的 if 语句中声明一个变量(或多个变量)的方法?

可以这样做:caseWM_COMMAND:if(WORDwNotifyCode=HIWORD(wparam)){...}可以这样做:caseWM_COMMAND:{WORDwNotifyCode=HIWORD(wparam);if(wNotifyCode>1){...}}但是不能这样做:caseWM_COMMAND:if((WORDwNotifyCode=HIWORD(wparam))>1){...}我认为在这里使用for语句是误导性的:caseWM_COMMAND:for(WORDwNotifyCode=HIWORD(wparam);wNotifyCode>1;wNotifyCode

c++ - 我们是否需要保护单个赋值或 if 线程安全语句

假设我有:staticintwrite_log=0;void*logger__run(void*arg){//loggerthreadexecution.while(1){//getlogmessagefromsharedqueue.if(write_log){//justcheckingwrite_logvalue.//writelogstillwrite_logistrue.}//destroylogmessage.}}voidlogger__set_logging(intp_write_log){//otherthreadscanstart/stoploggingbylogger