草庐IT

if-constexpr

全部标签

c++ - 函数参数的 MSVC 和 constexpr?

这段代码用clang和gcc编译得很好。templatestructN{staticconstexprsize_tv=n;};templateconstexprbooloperator,size_tn2){returnnconstexprvoidfoo(Nv){static_assert(v{});return0;}但是,如果我使用MSVC,我得到的错误是v不是常量表达式。我能理解为什么MSVC这么认为,但我认为这是错误的,而clang/gcc是正确的。是MSVC的错误吗? 最佳答案 是的,MSVC在这里是错误的。代码格式良好似乎违

c++ - "if the context from which the specialization is referenced depends on a template parameter"是什么意思?

根据C++17标准,[temp.point]/4,强调我的,Foraclasstemplatespecialization,aclassmembertemplatespecialization,oraspecializationforaclassmemberofaclasstemplate,ifthespecializationisimplicitlyinstantiatedbecauseitisreferencedfromwithinanothertemplatespecialization,ifthecontextfromwhichthespecializationisrefere

c++ - constexpr 上下文中的 std::optional 赋值运算符

我在std::optional上摸不着头脑,根据thedocs,不应有constexpr赋值运算符。但是,当我在gcc-8.1中尝试这个片段时,它编译并工作得很好:constexprstd::optionalfoo(){std::optionalbar=3;bar=1337;returnbar;}constexprautoz=foo();有什么我想念的吗? 最佳答案 这似乎是gcc中的一个错误。我刚刚在clang-6.0中尝试过,编译失败并出现预期错误。此外,该标准没有提及赋值运算符的任何constexpr重载,因此我会将此错误报告

c++ - 使用 move ctor 的 constexpr 对象的 constexpr 数组

我有一个带有constexpr值构造函数的类,但没有复制或移动构造函数classC{public:constexprC(int){}C(constC&)=delete;C&operator=(constC&)=delete;};intmain(){constexprCarr[]={1,2};}我发现这段代码不起作用,因为它实际上是在尝试使用C的移动构造函数而不是值构造函数来就地构造。一个问题是我希望此对象不可移动(出于测试目的),但我想“好吧,好吧,我将添加一个移动构造函数。”classC{public:constexprC(int){}C(constC&)=delete;C&oper

C++14:从参数值初始化 constexpr 变量

假设我有一个类可以通过constexpr函数返回常量表达式:templatestructFoo{constexprintBar()const{returnN;}};如果我想从Foo::Bar()初始化constexpr值,我应该如何传递类型为Foo的参数?我已经尝试了这两个,每个示例中都有一个constexpr变量来测试它是否可以被初始化:templateconstexprintByValue(Foof){constexprinti=f.Bar();returnf.Bar();}templateconstexprintByReference(constFoo&f){constexpri

c++ - 为什么删除默认参数会破坏此 constexpr 计数器?

考虑以下实现编译时间计数器的代码。#includetemplatestructFlag{friendconstexprintflag(Flag);};templatestructWriter{friendconstexprintflag(Flag){return0;}};templateconstexprintreader(float,Flag){returnN;}template{})>constexprintreader(int,Flag,intvalue=reader(0,Flag{})){returnvalue;}template{}),int=sizeof(Writer)>c

c++ - if 语句中的复合表达式

我偶然发现了执行此操作的能力。#includeusingnamespacestd;intmain(){if(({inti=1024;i==10;})){cout重要的反汇编区域好像是:->0x10000118f:movl$0x400,-0x18(%rbp);imm=0x4000x100001196:cmpl$0xa,-0x18(%rbp)0x10000119a:sete%al0x10000119d:andb$0x1,%al0x10000119f:movb%al,-0x19(%rbp)0x1000011a2:testb$0x1,-0x19(%rbp)0x1000011a6:je0x100

c++ - 将 constexpr 数组扩展为一组非类型模板参数

假设我有一个编译时constexpr数组和一个带有一组与数组元素类型相同的非类型参数的可变参数类模板。我的目标是用数组中的值实例化类模板:structContainer{intcontainee[3];};constexprContainermakeContainer();templateclassFoo;Foofoo;上面的代码运行良好。但是,每当我需要实例化Foo时都必须手动索引数组,这让我很不高兴。模板。我希望编译器自动为我执行此操作:Foofoo;我在cppreference上做了一些RTFM,但这没有帮助。我知道std::forward(),但它不能应用于模板参数列表。

c++ - If(), else if() c++中的替代方案(这是AI吗?)

首先,我是一个菜鸟。我也是一个从未通过编写代码赚过一毛钱的看门人。这只是我喜欢做的事情。这是为了好玩:)话虽这么说,我写了这个基于控制台的井字游戏,它有足够的人工智能,不会输掉每场比赛。(我想ai应该叫它什么。)它有大约70个if/elseif语句用于计算机。我像这样使用了3个int数组:intL[2],M[2],R[2];0=空白;1=X;2=O;董事会然后“看起来”像L[0]|米[0]|R[0]L[1]|米[1]|R[1]L[2]|米[2]|R[2]所以我基本上写出了我能想到的每一种可能的情况:if(M[0]==1&M[1]==1&M[2]==0){M[2]=2;}//hereth

c++ - 带有 enable_if 和重载的 SFINAE

我环顾四周,但无法找到解决我的具体问题的方法。我有代码:templatetypenamestd::enable_if::value||std::is_enum::value,std::string>::typeconvertToString(constTargument){returnstd::to_string(argument);}std::stringconvertToString(std::stringstring);代码应该做什么:对任何数字类型(int、float、double和ENum)使用模板版本,对其他任何类型使用std::string版本。代码本身编译得很好,但是当