有条件的constexpr成员函数 [英] Conditionally constexpr member function

查看:49
本文介绍了有条件的constexpr成员函数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

假设我有一个模板类

template <typename T>
class foo {
    T m;

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

如何给 foo:f()仅在 T :: f()是constexpr时使用constexpr说明符?

How can I give foo:f() the constexpr specifier only if T::f() is constexpr?

推荐答案

您只需在其上拍一个 constexpr

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

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

Yes, it's perfectly still valid even if T::f() isn't constexpr; such a function simply can't be used in constant expressions. See [dcl.constexpr]/7.

这篇关于有条件的constexpr成员函数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆