【问题标题】:Conditionally constexpr member function有条件的 constexpr 成员函数
【发布时间】:2017-01-07 03:50:55
【问题描述】:

假设我有一个模板类

template <typename T>
class foo {
    T m;

    decltype(auto) f() { return m.f(); }
};

如果T::f() 是 constexpr,我如何才能给 foo:f() constexpr 说明符?

【问题讨论】:

    标签: c++ templates c++14 constexpr


    【解决方案1】:

    你只需在上面打一个constexpr

    constexpr decltype(auto) f() { return m.f(); }
    

    是的,即使T::f() 不是constexpr,它仍然完全有效;这样的函数根本不能在常量表达式中使用。见[dcl.constexpr]/7

    【讨论】:

      猜你喜欢
      • 2020-03-10
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-06-09
      • 2016-07-29
      • 1970-01-01
      • 2019-07-05
      • 2011-08-06
      相关资源
      最近更新 更多