我正在尝试递归地运行一个类型列表,这样我就可以根据列表中的每种类型编写一些运行时代码。我希望能够在不使用“ifconstexpr”终止递归的情况下递归地遍历结构中函数(而不是结构中的函数)中的元组中的所有类型。我有一段代码显示了使用constexpr的递归。#include#include#includetemplatestructtemp{usingTypeList=std::tuple;constexprstaticstd::size_t_N=std::tuple_size::value;voidprint_this(){_inner_print();}templatevoid_i
尽管我的代码编译得很好,但这一直困扰着我,我无法在stackoverflow上找到答案。以下通用构造函数是将shared_ptr传递给构造函数中的类实例的一种方法。MyClass{MyClass(conststd::shared_ptr&pt);std::shared_ptrpt_;//EDITED:Removed&typo};MyClass::MyClass(conststd::shared_ptr&pt):pt_(pt){}这编译得很好。我的问题如下:在我的理解中,像这样声明一个参数const:voidmyfunc(constT&t)promise不改变t。但是,通过将shared
例如,除非声明incr()constexpr,否则下面的代码不会编译:intincr(int&n){return++n;}constexprintfoo(){intn=0;incr(n);returnn;}查看C++14中的§7.1.5/3我们有:Thedefinitionofaconstexprfunctionshallsatisfythefollowingconstraints:(3.1)—itshallnotbevirtual(10.3);(3.2)—itsreturntypeshallbealiteraltype;(3.3)—eachofitsparametertypessha
我有一个问题,我几乎可以肯定这是一个MSVC错误,但也许我遗漏了什么。这是实际代码的简化版本:templateclassInnerType{};templateclassOuterType{public:voidfoo1(){ifconstexpr(true){bar(InnerType());}}voidfoo2(){if(true){bar(InnerType());}}voidbar(InnerType){}};如您所见,foo1()和foo2()之间的唯一区别是constexprif。以下是我尝试在MSVC中编译一些测试时发生的情况:OuterTypetest1;test1.f
考虑以下定义。charright_string[]="::right_one.";charwrong_string[]="::wrong_one.";templatevoidf(){static_assert(str==::right_string,"Passme::right_string!");}structTest{staticconstexprcharright_string[]="template_struct::right_one";staticconstexprcharwrong_string[]="template_struct::wrong_one";template
我有以下C++11代码(简化版):structInfo{constchar*name;intversion;};classBase{public:constInfoinfo;Base(Infoinfo):info(info){}};classDerived:publicBase{public:staticconstexprInfoinfo={"Foobar",2};Derived():Base(info){}};intmain(){staticDerivedderived;return0;}GCC4.9.1可以很好地编译和链接这段代码。另一方面,Clang3.5.0提示undefine
我认为这是不可能的,但我想在放弃之前问问你。我想要类似constexpr增量的东西。#includeconstexprintinc(){staticintinc=0;returninc++;}classFoo{staticconstintType=inc();};classFoo2{staticconstintType=inc();};intmain(){std::cout我想将它调用到某些类中不是手动(为此我使用CRTP),为每个类提供不同的类型,但类型必须是常量。无论如何在C++中实现类似的东西?(C++17+TS) 最佳答案
大多数C++编译器支持SIMD(SSE/AVX)指令,其内部结构如_mm_cmpeq_epi32我的问题是这个函数没有被标记为constexpr,虽然“语义上”没有理由让这个函数不是constexpr,因为它是一个纯函数。有什么方法可以编写我自己的(例如)_mm_cmpeq_epi32版本,即constexpr?显然我希望该函数在运行时使用适当的asm,我知道我可以使用constexpr的慢函数重新实现任何SIMD函数。如果您想知道我为什么关心SIMD函数的constexpr。非constexprness具有传染性,这意味着我的任何使用这些SIMD函数的函数都不能是constexpr。
当类具有constexpr成员函数并且该成员函数正在constexpr上下文中的左值对象上求值时,clang和gcc不同意结果是否为constexpr值。为什么?是否有既不需要默认可构造性也不需要复制可构造性的解决方法?当对象按值传递时,两个编译器都编译成功。Clang版本trunk、8、7:static_assert表达式不是整数常量表达式和Gcc版本trunk、8.1、7.4:编译没有错误#includeusingA=std::array;voidfoo(constA&a){//clang:static_assertexpressionisnotanintegralconstant
以下程序将调用fun2^(MAXD+1)次。不过,最大递归深度永远不应超过MAXD(如果我的想法是正确的话)。因此编译可能需要一些时间,但它不应该占用我的RAM。#includeconstintMAXD=20;constexprintfun(intx,intdepth=0){returndepth==MAXD?x:fun(fun(x+1,depth+1)+1,depth+1);}intmain(){constexprinti=fun(1);std::cout问题是吃我的RAM正是它所做的。当我将MAXD调至30时,我的笔记本电脑在GCC4.7.2快速分配3GB左右后开始交换。我还没有尝