草庐IT

non-constexpr

全部标签

c++ - 为什么 Visual Studio 不允许我在 enable_if 中使用模板化的 constexpr 函数?

所以我将其归结为最小的、完整的、可验证的示例,而且VisualStudio2015似乎不允许我使用模板化的constexpr在enable_if中运行.例如:templateconstexprboolcondition(){returnsizeof(T)>1;}给我错误:errorC2995:enable_if::typetest(void):functiontemplatehasalreadybeendefined当我尝试在替换中使用它时,失败不是这样的错误编译:templateenable_if_t()>test(){coutenable_if_t()>test(){cout这在g

c++ - 数组初始化编译时间 - Constexpr 序列

我正在阅读this关于SO的问题。这个问题本身并没有那么有趣,但我想知道它是否存在以及如何实现编译时解决方案。关于第一个序列:Allnumbersexcepttheoneswhichcanbedividedby3.顺序应该是这样的:[1,2,4,5,7,8,10,11,13,14,...]通过归纳法,我找到了该序列的数学公式:f(0)=0;f(x>0)=floor[(3x-1)/2];所以我实现了一个C++constexpr函数,它生成序列中的第i-th个数:#includetemplateconstexprTgenerate_ith_number(conststd::size_tin

c++ - 寻找一个 constexpr ceil 函数

由于std:ceil在VisualStudio2015中都不是constexpr,我正在寻找它的constexpr实现可以使用编译时-收效甚微。感谢任何帮助。 最佳答案 由于VisualStudio2015的编译器仍然不允许constexpr函数具有if条件和变量,我重写了Jarod42的解决方案并删除了它们:constexprint32_tceil(floatnum){return(static_cast(static_cast(num))==num)?static_cast(num):static_cast(num)+((num

c++ - 为什么不能实例化带有 "non const"复制构造函数的对,而没有实例化一对是可能的?

假设您有以下类(class):structA{A(){}A(A&)=delete;};intmain(){std::pairp1;return0;}以下代码将无法编译(使用-std=c++11和g++)并出现以下错误:/usr/include/c++/5/bits/stl_pair.h:Ininstantiationof‘structstd::pair’:test.cpp:13:23:requiredfromhere/usr/include/c++/5/bits/stl_pair.h:127:17:error:‘constexprstd::pair::pair(conststd::pa

c++ - 在 C++11 中的编译时 (constexpr) 计算斐波那契数(递归方法)

我在编译时写了斐波那契数计算程序(constexpr)使用C++11支持的模板元编程技术的问题。目的这是为了计算模板元编程方法与旧的传统方法之间的运行时间差异。//TemplateMetaprogramingApproachtemplateconstexprintfibonacci(){returnfibonacci()+fibonacci();}templateconstexprintfibonacci(){return1;}templateconstexprintfibonacci(){return0;}//ConventionalApproachintfibonacci(intN

c++ - 使用 clang 的 constexpr 深度限制(constexpr-depth 似乎不起作用)

有没有办法配置constexpr实例化深度?我正在运行-fconstexpr-depth=4096(使用clang/XCode)。但仍然无法编译此代码并报错:Constexpr变量fib_1必须由常量表达式初始化。无论是否设置选项-fconstexpr-depth=4096,代码都会失败。这是clang的错误还是预期的行为方式。注意:这在fib_cxpr(26)之前一直有效,27是它开始失败的时候。代码:constexprintfib_cxpr(intidx){returnidx==0?0:idx==1?1:fib_cxpr(idx-1)+fib_cxpr(idx-2);}intmai

c++ - constexpr 和 std::cout 处理函数但不处理 lambda

为什么constexpr不适用于std::cout,但适用于printf?#includeconstexprvoidf(){std::cout为什么std::cout与lambdasconstexpr一起工作?#includeintmain(){autoh=[]()constexpr{std::cout 最佳答案 从技术上讲,它不适用于其中任何一个。来自[dcl.constexr]:Foraconstexprfunctionorconstexprconstructorthatisneitherdefaultednoratemplat

c++ - 错误 : invalid initialization of non-const reference of type ‘bool&’ from an rvalue of type ‘std::vector<bool>::reference {aka std::_Bit_reference}’

为什么我会收到错误:从类型为“std::vector::reference{akastd::_Bit_reference}”的右值对类型为“bool&”的非常量引用进行无效初始化?vector>vis;bool&visited(intx,inty){returnvis[x][y];//error}据我所知,vector中的operator[]返回引用,所以它应该是一个左值,但它不起作用。我应该怎么做才能让它发挥作用? 最佳答案 那是因为std::vector不是它看起来的样子。std::vector有一个特化与类型bool-它是空间

c++ - 在 constexpr 函数中返回一个 C 字符串 : why no warning from the compiler?

考虑以下代码:constexprautof(){autostr="HelloWorld!";returnstr;}intmain(intargc,char*argv[]){staticconstexprautostr=f();std::cout我的编译器不显示任何警告是否正常?它是定义的行为吗?我能保证程序会显示"HelloWorld!"吗?我希望"HelloWorld!"不会超出函数的范围... 最佳答案 在C++中stringliterals有staticstorageduration只要程序运行就可以生存。因此,指向从f返回的

c++ - 基于模式创建位掩码作为 constexpr

我想实现一个模板函数,它在编译时为整数类型生成位掩码。这些掩码应基于8位模式,其中模式将连续重复以填充整数。以下示例完全符合我的要求,但在运行时:#include#include#includetemplatetypenamestd::enable_if::value,Int>::typemake_mask(unsignedcharpattern){Intoutput{};std::memset(&output,pattern,sizeof(Int));returnoutput;}intmain(){automask=make_mask(0xf0);std::cout上面代码的输出是: