草庐IT

if-constexpr

全部标签

c++ - fatal error C1017 : invalid integer constant expression when using "#if (false)"

下面的代码可以在Linux下运行,但对于MSVS会出错#if(false)....#endif错误是:fatalerrorC1017:invalidintegerconstantexpression我在Microsoft的网站上找到了这份报告:http://msdn.microsoft.com/en-us/library/h5sh3k99.aspx虽然那里描述的信息与我的情况相比略有不同,因为我没有使用“#define”所以我的问题是:有没有什么方法可以让它在不更改代码的情况下为MSVC工作?如果必须更新代码,这种情况的最佳解决方案是什么? 最佳答案

c++ - 为什么在 gcc 中允许带有 std::string 的模板化 constexpr?

为什么gcc允许编译模板版本?它是编译器错误还是与模板一起使用时实际上有效?有人可以给我解释一下吗?它不能在clang或godbolt.org上使用的其他编译器上编译。编译错误是由在constexpr中使用的字符串和字符串流产生的。#include#include#includetemplateconstexprstd::stringfunc1(Ta,Tb)//Compilesandruns{std::stringstreamss;ss 最佳答案 GCC可能就在这里。根据dcl.constexpr第6段:Iftheinstantia

c++ - 为什么这个enable_if函数模板不能专用于VS2017?

以下使用VS2015编译,但在VS2017中失败并出现以下错误。代码是否在做一些非标准的事情,已在VS2017中修复,或者VS2017应该编译它?#include"stdafx.h"#includetemplateconstexprautoToUnderlying(Ee){returnstatic_cast>(e);}templateboolconstexprIsFlags(T){returnfalse;}templatestd::enable_if_t>operator|(Elhs,Erhs){returnToUnderlying(lhs)|ToUnderlying(rhs);}en

c++ - const 可以,但不是 constexpr?

使用constexpr指定的函数foo_constexpr我有如下所示的代码:constautox=foo_constexpr(y);static_assert(x==0);当x的声明更改为constexpr时,在什么情况下代码可能无法编译?(毕竟,x必须已经是用于static_assert的常量表达式。)即:constexprautox=foo_constexpr(y);static_assert(x==0); 最佳答案 在一般中,当foo_constexpr的执行违反常量表达式的要求时,它可能无法编译。请记住,constexpr

c++ - decltype(constexpr 变量)

为什么constexpr变量的decltype失败?#include#includeconstexpruint16_tfoo(){return0;}constexprautocv=foo();autov=foo();static_assert(std::is_same::value,"!");//failedstatic_assert(std::is_same::value,"!");//success 最佳答案 decltype(entity)指定此表达式指定的实体的声明类型。由于constexpr,(对象声明中使用的conste

c++ - 内联函数指针以避免 if 语句

在我的jpg解码器中,我有一个带有if语句的循环,该语句将始终为真或始终为假,具体取决于图像。我可以创建两个单独的函数来避免if语句,但出于好奇我想知道使用函数指针而不是if语句对效率有什么影响。如果为真,它将指向内联函数;如果为假,它将指向一个空的内联函数。classjpg{private://emtpyfunctionvoidinlinenothing();//realfunctionvoidinlinefunction();//pointertoinlinefunctionvoid(jpg::*functionptr)()=nullptr;}jpg::nothing(){}mai

c++ - std::remove_if 中的 const 参数

我要从对列表中删除元素。当我使用一对像std::pair我得到以下编译错误:Infileincludedfrom/usr/local/include/c++/6.1.0/utility:70:0,from/usr/local/include/c++/6.1.0/algorithm:60,frommain.cpp:1:/usr/local/include/c++/6.1.0/bits/stl_pair.h:Ininstantiationof'std::pair&std::pair::operator=(std::pair&&)[with_T1=constint;_T2=bool]':/u

c++ - 在 if...else 语句中嵌入 case 标签

G++接受此代码并且它的行为符合我的预期:#includevoidexample(intvalue,boolcondition){switch(value){case0:if(condition){case1:assert(condition||value==1);}else{assert(!condition&&value==0);}assert(value==0||value==1);}}intmain(){example(0,false);example(1,false);example(0,true);example(1,true);}也许这是一个愚蠢的基本问题,但撇开代码的味

c++ - static const 和 constexpr 变量有什么区别?

我知道constexprvariable可以在编译时使用。对于模板,或者静态断言。但是如果我想在没有constexpr的情况下做到这一点,我可以使用staticconst。C++11/14引入constexpr后有什么区别constexprinta=3;//ANDstaticconstinta=3;谢谢!查看此问题的另一种方式是我应该使用哪个? 最佳答案 我所知道的主要区别是,constexpr的值必须在编译时已知,而conststatic可以在运行时分配。conststaticintx=rand();

c++ - constexpr 默认的默认构造函数

如果我想将我的default编辑的默认构造函数声明为constexpr,我会收到Clang3.8和GCC5.3的编译器错误。根据thisstackoverflow问题它应该可以正常工作:structA{constexprA()=default;intx;};但是:Error:defaulteddefinitionofdefaultconstructorisnotconstexpr你知道到底发生了什么吗? 最佳答案 就目前而言,x保持未初始化状态,因此无法在编译时构造对象。你需要初始化x:structA{constexprA()=de