草庐IT

if-constexpr

全部标签

c++ - "warning: operation of ... may be undefined"用于三元运算——不是 if/else block

这个问题在这里已经有了答案:Undefinedbehaviorandsequencepoints(5个答案)关闭6年前。这是我的代码:intmain(){staticinttest=0;constintanotherInt=1;test=anotherInt>test?test++:0;if(anotherInt>test)test++;elsetest=0;return0;}这是我构建它时产生的警告:../main.cpp:15:40:warning:operationon‘test’maybeundefined[-Wsequence-point]test=anotherInt>te

c++ - 在常量表达式上下文中定义之前的嵌套 `constexpr` 函数调用

根据我从thisanswer收集到的信息,如果函数尚未声明,则constexpr函数的结果不是常量表达式。令我惊讶的是以下代码片段:constexprintf();constexprintg(){returnf();}constexprintf(){return42;}intmain(){constexprinti=g();returni;}这个编译没有问题并且可以工作。将f的定义移动到主触发器之后error:'constexprintf()'usedbeforeitsdefinition,如我所料。我认为它可以工作,因为f已在调用g之前定义,因此这两个调用都是常量表达式。为什么f()

c++ - constexpr lambda/‘x’ 没有命名类型;你是说 ‘x’ 吗?

我正在尝试使用C++17的constexprlambdas来获取编译时字符串:#includetemplatestructstr{constexprautooperator==(conststr&)const{returntrue;}voidfoo()const;};templateconstexprautomake_str(Ss,std::index_sequence){returnstr{};}#defineLIT(s)\make_str([](){returns;},std::make_index_sequence{})constexprautox=LIT("hansi");co

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

c++ - 关于成员函数指针值的 Constexpr - 未定义的行为?

我对C++中类似机制的简单反射有疑问。我想要一种模板类型,它应该以不同的成员函数指针作为模板参数表现不同:[解决方案#1,按标准来说是不好的]如果我有一个带有类类型及其成员函数指针的类模板,我不能部分特化为null的成员指针,因为我不能特化“具有依赖类型的非类型模板参数”(参见:https://en.cppreference.com/w/cpp/language/partial_specialization参数列表[5])templatestructp{};templatestructp{};[解决方案#2,GCC问题]如果我尝试专注于一个推导的constexpr值,它反射(refle

c++ - gcc8.2 和 (intel) icc19.0.1 之间的 constexpr 差异

以下代码在gcc8.2上编译但在icc19.0.1上编译失败:#includetemplateconstexprsize_tf(std::tupleconst&){return0;}templatesize_tg(Tuple&&t){staticsize_tconstexprv=f(t);returnv;}size_th(){std::tupletuple;returng(tuple);}我从icc收到的错误是:error:expressionmusthaveaconstantvaluestaticsize_tconstexprv=f(t);^note:thevalueofparame

c++ - 如何将字符串列表存储在 constexpr 上下文中?

我一直在为某些域对象类转换这些文档字符串表(作为系统的类型特征),直到我偶然发现了这个问题。后来,我打算在编译时检查这些特殊成员是否编写了文档(作为我在编译时喜欢它的原因)。我创建了一个小示例用于演示:https://godbolt.org/z/3dX3-e#includestructCStr{structM{constchar*name;constchar*val;};constexprCStr(conststd::initializer_list&str):str_(str){};std::initializer_liststr_;};constexprCStrcstr_test{

在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