草庐IT

if-constexpr

全部标签

c++ - constexpr 和 CRTP : compiler disagreement

当使用CRTP实现表达式模板时,位于表达式层次结构顶部的类使用基到派生的向下转型来实现它的一些操作。根据clang-3.5(-std=c++1y),这种向下转换在constexpr函数中应该是非法的:test.cpp:42:16:error:static_assertexpressionisnotanintegralconstantexpressionstatic_assert(e()==0,"");^~~~~~~~test.cpp:11:26:note:cannotcastobjectofdynamictype'constbase'totype'constderived'constn

c++ - if(double) 有效的 C++ 吗?

我刚遇到这行代码:if(lineDirection.length2()){...}其中length2返回一个double。让我有点困惑的是0.0等同于0、NULL和/或false。这是C++标准的一部分还是未定义的行为? 最佳答案 这是一个非常标准的行为(bool转换)$4.12/1-"Anrvalueofarithmetic,enumeration,pointer,orpointertomembertypecanbeconvertedtoanrvalueoftypebool.Azerovalue,nullpointervalue,

c++ - 数组的 constexpr 初始化以对内容进行排序

这有点像一个谜而不是一个现实世界的问题,但我已经遇到了这样一种情况,我希望能够编写一些行为完全相同的东西templatestructSortMyElements{intdata[N];templateSortMyElements(TT...tt):data{tt...}{std::sort(data,data+N);}};intmain(){SortMyElementsse(1,4,2,5,3);intse_reference[5]={1,2,3,4,5};assert(memcmp(se.data,se_reference,sizeofse.data)==0);}除了我想要SortM

c++ - 通过显式转换函数初始化枚举类类型的静态 constexpr 类成员

我在g++4.8.1和clang++3.4的行为之间存在差异。我有一个A类,它是文字类型的,它有一个explicitconstexpr转换函数来输入enumclassE.Gcc允许我在某些情况下使用转换函数从A类型的常量表达式初始化constexpr类型的E变量,但是不是当变量是静态类成员时(下面的e2)Clang拒绝所有上下文中的初始化(e1、e2和e3)。根据[over.match.conv]p1,在这里可以使用显式转换函数enumclassE{e};structA{explicitconstexproperatorconstE()constnoexcept{returnE::e;

c++ - 声明 constexpr 函数或方法

我想知道是否对必须声明constexpr函数和方法的位置有任何限制,就像内联函数和方法一样。我知道内联函数或方法必须写在头文件中,以便编译器可以在调用它们的地方访问它们的定义。如果constexpr有类似的东西,那将是有道理的,但我无法在这一点上找到任何东西......所以基本上我的问题是:我能否在头文件中编写constexpr函数的定义而不冒重复符号的风险?我可以将constexpr函数或方法的声明和定义分开吗? 最佳答案 您定义constexpr函数的位置会影响您如何使用它。特别是:C++14[expr.const]p2:Aco

c++ - SFINAE enable_if 显式构造函数

我正在尝试通过enable_if在显式和隐式转换构造函数之间切换。我的代码目前看起来像#include#includeenumclassenabled{};templateusingenable_if_t=typenamestd::enable_if::type;templateusingdisable_if_t=typenamestd::enable_if::type;templatestructSStruct{staticconstexprstd::intmax_ta=A;};templatestructSCheckEnable:std::integral_constant{};t

c++ - 是否可以使用 `constexpr` 模板变量作为正式模板参数的默认值

使用clang3.6.0,我无法编译以下代码示例。#includetemplateconstexprboolIS_SCALAR=::std::is_scalar::value;template>structClass_Breaks{};template::value>structClass_Works{};voidfunction(){Class_Breaksbreak_error;Class_Breaks>breaks_ok;Class_Worksok;}但是,返回以下错误消息:1>[66%]BuildingCXXobjectCMakeFiles/Core.dir/tests.cpp

c++ - 使用 GCC 优化 C/C++ 循环中的嵌套 if 语句

我正在使用GCC编译器测试C/C++中的各种优化。我目前有一个包含多个嵌套if语句的循环。条件是在程序开始执行时计算的。它看起来有点像这样:boolconditionA=getA();boolconditionB=getB();boolconditionC=getC();//Etc.startTiming();do{if(conditionA){doATrueStuff();if(conditionB){//Etc.}else{//Etc.}}else{doAFalseStuff();if(conditionB){//Etc.}else{//Etc.}}}while(testCondi

c++ - 不使用这个的 constexpr 成员函数?

请考虑以下两个C++14程序:程序1:structS{constexprintf()const{return42;}};Ss;intmain(){constexprintx=s.f();returnx;}程序2:structS{constexprintf()const{return42;}};intg(Ss){constexprintx=s.f();returnx;}intmain(){Ss;returng(s);}这些程序中的一个或两个都不是良构的吗?为什么/为什么不? 最佳答案 这两个程序都是良构的。C++14标准要求s.f()

c++ - decltype(foo(1)) 应该实例化 constexpr 函数模板 foo 吗?

以下代码使用gcc和MSVC编译,但使用clang失败,我使用clang-3.5和当前主干进行了测试。templateconstexprautowrong=false;templateconstexprautofoo(constTt)->int{static_assert(wrong,"");return{};}usingF=decltype(foo(1));intmain(){}clang实例化函数体并偶然发现static_assert。gcc和MSVC只看函数声明,忽略函数体中的static_assert。如果删除constexpr,所有编译器都能正常编译代码。问题:如果声明了返回