我知道const和constexpr之间的区别。一个是编译时常量,另一个是编译时常量或运行时常量。但是,对于字符/字符串数组,我很困惑为什么编译器会提示一个被使用在另一个之上。例如我有:constexprchar*A[2]={"....","....."};constconstexprchar*B[2]={"....","....."};通过声明“A”我得到:ISOC++forbidsconvertingastringconstantto'char*'[-Wwrite-strings]但声明为“B”时,我没有收到任何警告。为什么额外的const限定符会消除警告?无论如何,它们都不都是“
在C++11或C++14中,我试图为constexpr函数定义类型别名。我试过:#includeconstexprintfoo(inti,intj){returni+j;}usingTConstExprFunction=constexprint(*)(inti,intj);intmain(){TConstExprFunctionf=foo;constexprinti=f(1,2);std::cout但它无法使用g++和clang++进行编译。g++:错误:“constexpr”之前需要类型说明符clang++:错误:类型名称不允许指定constexpr说明符我必须按照下面的方式进行编译
当使用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
我刚遇到这行代码:if(lineDirection.length2()){...}其中length2返回一个double。让我有点困惑的是0.0等同于0、NULL和/或false。这是C++标准的一部分还是未定义的行为? 最佳答案 这是一个非常标准的行为(bool转换)$4.12/1-"Anrvalueofarithmetic,enumeration,pointer,orpointertomembertypecanbeconvertedtoanrvalueoftypebool.Azerovalue,nullpointervalue,
这有点像一个谜而不是一个现实世界的问题,但我已经遇到了这样一种情况,我希望能够编写一些行为完全相同的东西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
我在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;
我想知道是否对必须声明constexpr函数和方法的位置有任何限制,就像内联函数和方法一样。我知道内联函数或方法必须写在头文件中,以便编译器可以在调用它们的地方访问它们的定义。如果constexpr有类似的东西,那将是有道理的,但我无法在这一点上找到任何东西......所以基本上我的问题是:我能否在头文件中编写constexpr函数的定义而不冒重复符号的风险?我可以将constexpr函数或方法的声明和定义分开吗? 最佳答案 您定义constexpr函数的位置会影响您如何使用它。特别是:C++14[expr.const]p2:Aco
我正在尝试通过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
使用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
我正在使用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