jjzjj

constexpr

全部标签

c++ - 非成员函数 begin()/cbegin() 及其constexpr-ness

C++11引入了没有constexpr-说明符的std::begin()非成员函数,然后C++14更新为constexpr-std::begin()用于数组类型(T(&)[N])并附加constexpr-std::cbegin()用于通用容器类型(constC&).引自http://en.cppreference.com/w/cpp/iterator/begintemplateconstexprT*begin(T(&array)[N]);//(sinceC++14)templateconstexprautocbegin(constC&c)->decltype(std::begin(c)

c++ - 为什么 static const char * template struct 成员没有初始化

我有一些C++11模板代码,我正在尝试移植到VisualC++Compiler2015。原始代码工作得很好,但是我需要重写它以解决constexpr的问题。Theoriginalcode(simplifiedexample)#includestructString{staticconstexprconstchar*value{"STRING"};};templateclassDerived{public:staticconstexprconstchar*value{Base::value};};templatestructFoo{staticconstexprconstchar*val

c++ - 具有未触及的非 constexpr 参数 : Who is correct, clang 或 gcc 的 constexpr?

我有4个测试用例,我相信它们都是有效的:constexprintf(intconst&/*unused*/){return1;}voidg(intconst&p){constexprinta=f(p);//clangerror,gccvalidintv=0;constexprintb=f(v);//clangvalid,gccvalidintconst&r=v;constexprintc=f(r);//clangerror,gccerrorintn=p;constexprintd=f(n);//clangvalid,gccvalid}intmain(){intp=0;g(p);}Cla

c++ - SFINAE 在评估模板参数中的 constexpr 时失败?

出于某种原因,此constexpr在模板参数上下文中未被正确评估:#include#includenamespacedetail{//Reasontouseanenumclassrahterthanjustanintissoastoensure//therewillnotbeanyclashesresultinginanambigiousoverload.enumclassenabler{enabled};}#defineENABLE_IF(...)std::enable_if_t=detail::enabler::enabled#defineENABLE_IF_DEFINITION(

c++ - 如何在不重复代码的情况下使用 if-constexpr?

目前我在做:ifconstexpr(constexpr_bool_var1){autoarg1=costly_arg1();autoarg2=costly_arg2();if(costly_runtime_function(arg1,arg2)){//doX,possiblymoreconstexprconditions//doY//...}}else{//doX,possiblymoreconstexprconditions//doY//...}一种可能的方法是将doX/Y等转换为一个函数doXY()并在两个地方调用它,但是它看起来很笨拙,因为我必须编写一个函数,它只存在于方便元编程

c++ - 在某些情况下允许从 constexpr 调用非 constexpr 函数

来自那个问题:Howtobuildacustommacrothatbehavesdifferentlywhenusedasconstexpr(likeassert)?我想知道如果有条件的话为什么可以调用非constexpr函数。voidbla(){std::coutnotconstexpr!condition?void(0):bla();//compilesandrunsevenifconditionistrueorfalse!//ifconditionisconst,itdidnotcompilebecauseit//directlyforceexecutionofnonconste

c++ - constexpr 构造函数不会显示覆盖率数据

今天我将矩阵类重写为constexpr。我对这个类有100%的单元测试覆盖率,但我注意到在我将几乎所有函数转换为constexpr之后,构造函数的一部分在lcov中被标记为根本不再被覆盖。这是只有构造函数的类。templateclassMatrix{static_assert(std::is_arithmetic::value,"Matrixcanonlybedeclaredwithatypewhere""std::is_arithmeticistrue.");public:constexprMatrix(std::initializer_list>matrix_data){if(ma

c++ - 调用模板参数 constexpr 方法?

std::array::size()是非staticconstexpr方法;正在constexpr我可以将它用作模板参数:#include#includeintmain(void){std::arraya={{"Helloworld"}};std::coutb=a;std::cout但是,如果std::array是一个模板参数,事情变得不确定:templatevoidcopy(Tconst&a){std::arrayc=a;std::coutGCC愉快地编译这个[1],而CLANG拒绝[2]:6:20:error:non-typetemplateargumentisnotaconsta

c++ - 导致运行时执行的 constexpr 仿函数中的成员

我正在使用仿函数以下列方式生成编译时计算代码(对于长代码我深表歉意,但这是我发现重现该行为的唯一方法):#include#includetemplateconstexprautocompute(constdoubleh){std::tuple,std::array>paw{};autoxtab=std::get(paw).data();autoweight=std::get(paw).data();ifconstexpr(order==3){xtab[0]=-1.0E+00;xtab[1]=0.0E+00;xtab[2]=1.0E+00;weight[0]=1.0/3.0E+00;we

c++ - const_cast 在常量表达式中有效吗? (C++14, C++17)

a遇到的具体问题是编译器处理它的方式存在一些不一致。例如这段代码(https://godbolt.org/z/08Z-zi):constexprautovalue=1;static_assert(*const_cast(&value),"valueshouldbe1");使用GCC、Clang和MSVC编译良好,但使用英特尔C++编译器19.0.1失败并出现以下错误:error:expressionmusthaveaconstantvaluestatic_assert(*const_cast(&value),"valueshouldbe1");据我所知,标准并未明确声明常量表达式中不允