草庐IT

ConstexPR

全部标签

c++ - 构造函数何时调用 const-expr?

什么时候构造函数调用const-expr什么时候不是?在这个链接问题中Arethesevectordefinitions"constantinitialization"?构造函数不是const-expr,这就是vector未const初始化的原因。还有人可以更详细地解释该问题的答案吗? 最佳答案 您是在问何时可以在需要常量表达式的上下文中使用构造的结果?例如classA{constexprA(...){...};constexprintget(){...};...}constexprAa(...);std::arrayx{};换句话

c++模板扣除头文件中指定模板的数组大小

我正在阅读Meyers关于现代C++的书,我发现其中的代码片段可能很有用:templateconstexprstd::size_tarraySize(T(&)[N])noexcept{returnN;}这个函数推导N对我们来说是一个编译时常量。所以我想在我的代码中应用它:templateconstexprstd::size_tarraySize(T(&)[N])noexcept{returnN;}templateclassA{public:conststaticchar*names[];};templateconstchar*A::names[]={"foo","bar"};templ

c++ - 在编译时初始化 c++ std::bitset

我正在尝试初始化std::bitset在编译时使用它的一些索引,假设50-75和200-225设置为1。基于http://en.cppreference.com/w/cpp/utility/bitset/bitset看起来我的2个选项是:constexprbitset();constexprbitset(unsignedlonglongval);考虑到第二个构造函数不适用于大索引,有人可以阐明我将如何初始化我的位集吗? 最佳答案 Bitsetconstructorconstexprbitset(unsignedlonglongval

c++ - 使用 constexpr C 字符串作为编译器错误消息

正如问题所暗示的,我想做的是templatestructbroken{templateconstexprbroken(Args&&...){//thesizeof...confusesthecompilerastoonlyemiterrorswheninstantiated//thisdoesnotwork,static_assertonlyacceptsstringliteralsstatic_assert(sizeof...(Args)我希望broken在实例化时发出编译器错误消息Err。但是,static_assert只接受一个字符串文字作为它的第二个参数。有什么方法可以根据co

c++ - 编译时将树转换为元组

我遇到了以下问题:给定一棵由Node类型的非终端节点表示的树和任意类型的终端节点,如A,B等等(见下文)。因为我不想使用运行时多态性,所以我喜欢将树转换为std::tuple通过constexpr其功能类似于下面示例中立即调用的lambda表达式。structA{};structB{};structC{};structD{};structE{};templatestructNode{constexprNode(constT&...n):mChildren{n...}{}std::tuplemChildren;};templatestructIndexNode{std::arraymCh

c++ - 将 `hana::string` 转换为 `constexpr const char (&)[]`

我有一些旧代码使用了与str_const非常相似的东西描述here和here做一些constexpr字符串操作。str_const是ScottSchurr描述的文字类型,可以从字符串文字构造,因为它具有来自constchar(&)[]的模板构造函数.我现在也有一些使用boost::hana的新代码.我希望能够参加hana::string并创建一个str_const那是指它。最简单的方法是转换hana::string到constexprconstchar(&)[].(实际上,在这一点上这不是最简单的方法,最简单的方法肯定是在我的str_const实现中添加一个新的模板构造函数。但在这一点

c++ - 交换运算符的模板实现,合法吗?

我已经尝试为我的一个类实现交换加法运算符:structmytype{constexprmytype(othertypeconst&);constexprmytypeoperator+(othertypeconst&rhs)const;};templateconstexprautooperator+(Tconst&lhs,mytypeconst&rhs)->decltype(rhs+lhs){returnrhs+lhs;}想法是,只要右侧是mytype,右侧接受的任何内容在左侧也可以接受。这适用于icc和VisualStudio,并进入无限递归解析gcc和clang上的decltype(

c++ - MSVC 无法识别带有模板自动参数的 constexpr 函数

我试图为值列表中的值创建一个索引元查找器。这是代码:#include#includetemplatestructValueTplList;templateconstexprintMetaFindV(intind){//notfoundcasereturn-1;}templateconstexprintMetaFindV(intind=0){ifconstexpr(std::is_same_v){returnNeedleV==V?ind:MetaFindV(ind+1);}else{returnMetaFindV(ind+1);}}//maintemplatestructMetaInde

c++ - `inline` , `constexpr` 或 `noexcept` 仅声明函数

对于没有主体的函数,仅用于类型检查目的或在非评估上下文中,是否有任何冒险将此类函数标记为inline,noexcept或constexpr?例如:namespace_detail{templateconstexprRresult_type(R(T::*)(Params...))noexcept;templateconstexprRresult_type(R(T::*)(Params...)const)noexcept;templateconstexprRresult_type(R(*)(Params...))noexcept;templateconstexprRresult_type(

c++ - gcc 4.7 和递归 constexpr 函数

我正在玩constexpr递归并尝试查看它是如何编译的,我不明白在什么情况下gcc选择在编译时或运行时计算递归。我正在使用以下阶乘计算代码:#includeconstexprunsignedintfactorial(unsignedinti){returni>0?i*factorial(i-1):1;}intmain(void){std::cout然后我更改值x在阶乘中。在没有优化的情况下编译时,表达式不会在编译时计算。使用-O1编译时标志,表达式在编译时仍未计算。与-O2,如果x,表达式是在编译时计算的.在这个值之后,阶乘被内联实现为一个循环。更改-fconstexpr-depth的