草庐IT

if-constexpr

全部标签

c++ - 在编写平台相关代码时使用 constexpr if 而不是宏?

现在ifconstexpr是C++17的一部分,在编写平台相关代码和类似代码时,它是否是宏的良好替代品?我想知道,因为我真的不喜欢宏,并且只想将它们用于includeguards和includes。//thosevariablesshouldbegivenbythecompilerconstexprunsignedint__os=0x1;//currentosconstexprunsignedint__os_win=0x1;//Windowsconstexprunsignedint__os_linux=0x2;//Linux-flavorsconstexprunsignedint__o

c++ - 如果 count() 是 constexpr 函数,为什么 std::array<int, count()> 不能编译?

这个问题在这里已经有了答案:constexprnotworkingifthefunctionisdeclaredinsideclassscope(3个回答)3年前关闭。为什么下面的C++代码不能用VC2017编译?structFixedMatchResults{staticconstexprstd::size_tcount(){return20;};std::arrayresults;};错误是:errorC2975:'_Size':invalidtemplateargumentfor'std::array',expectedcompile-timeconstantexpression

c++ - constexpr 中的 std::variant 修改

考虑以下两个程序:#include#includeconstexprautof(){usingT=std::variant;Tt(false);t=T(true);returnstd::get(t);}templatevoidprint(){std::cout();}和#include#includeconstexprautof(){usingT=std::variant;Tt(false);t=T(42);returnstd::get(t);}templatevoidprint(){std::cout();}GCC编译这两个并输出预期的结果。在这两种情况下,Clang都不会编译它们中

c++ - 使用 constexpr 初始化数组?

我想知道是否可以使用constexpr函数(使用C++2011)初始化整个数组。在这里,我有一些东西可以说明我想做什么:templateconstunsignedintMyClass::_myVar[2][3]={{metaFunction(0,0,DIM),metaFunction(0,1,DIM),metaFunction(0,2,DIM)},{metaFunction(1,0,DIM),metaFunction(1,1,DIM),metaFunction(1,2,DIM)}};templateinlineconstexprunsignedintMyClass::metaFunct

c++ - 混合 decltype 和 enable_if

似乎将decltype与SFINAEenable_if一起使用并不简单。我尝试以三种不同的方式使用enable_if编写go。所有这些都因编译器错误而失败(GCC的字面意思是:“错误:'thing'不是'foo'的成员”和实例化上下文)。#includestructfoo{enum{has_thing=false};};structbar{enum{has_thing=true};staticintthing(){return0;}};templatestructTest{/*autogo(typenamestd::enable_if::type=0)->decltype(T::thi

c++ - std::move_if_noexcept 的基本原理仍在 move 抛出仅 move 类型?

move_if_noexcept将:返回一个右值——促进move——如果move构造函数是noexcept或者如果没有复制构造函数(仅move类型)返回一个左值——强制复制——否则我发现这相当令人惊讶,因为具有抛出move-ctor的仅move类型仍将由使用move_if_noexcept的代码调用此move-ctor。是否对此给出了详尽的理由?(也许直接或在N2983的两行之间?)代码不编译而不是仍然不得不面对不可恢复的move场景会不会更好?N2983中给出的vector示例很好:voidreserve(size_typen){......new((void*)(new_begin

c++ - 隐含的constexpr?

C++11编译器能否(它们确实)注意到一个函数是一个constexpr并且即使它们没有被声明为constexpr也如此对待它们?我正在向使用直接来自维基百科的示例的人演示constexpr的用法:intget_five(){return5;}intsome_value[get_five()+7];//Createanarrayof12integers.Ill-formedC++令我惊讶的是,编译器没有问题。因此,我进一步更改了get_five()以获取一些int参数,将它们相乘并返回结果,同时仍未明确声明为constexpr。编译器也同意这一点。似乎如果编译器可以做到这一点,那么为了显

c++ - 为什么我需要在 constexpr 类中对非静态数组成员进行成员初始化?

下面的代码迫切需要:values()来编译,至少在ideone::C++14中是这样:#includetemplatestructTable{constexprTable():values(){for(autoi=0;i();for(autox:a.values)std::cout但是为什么?我有过这样的想法:“值也可以以非constexpr方式初始化,而values()确实明确表示我们以符合constexpr的方式初始化它”。但是省略:values()是不是同样清楚? 最佳答案 考虑语义。从初始化列表中省略成员将执行默认初始化,在

c++ - 变量上的 `const constexpr` 是多余的吗?

cppreference指出:Aconstexprspecifierusedinanobjectdeclarationornon-staticmemberfunction(untilC++14)impliesconst.“对象声明”是指“任何变量声明”吗?即是constexprconstintsomeConstant=3;相当于constexprintsomeConstant=3;在C++11、C++14和C++17中? 最佳答案 在带有原语的声明中,例如您示例中的声明,const确实是多余的。但是,可能会有一些奇怪的情况需要con

c++ - 当迭代器(输入参数)通常不是 constexpr 时,constexpr 算法真的有用吗?

在c++20中提出,部分算法是constexpr。例如:templateboolall_of(InputItfirst,InputItlast,UnaryPredicatep);(sinceC++11)(untilC++20)templateconstexprboolall_of(InputItfirst,InputItlast,UnaryPredicatep);(sinceC++20)虽然我们知道迭代器通常不是constexpr。我认为这仅在constexpr容器的情况下有用。有人可以澄清我是否遗漏了什么以及我的理解是否正确吗? 最佳答案