Conditionally constexpr member function

岁酱吖の 提交于 2019-12-22 08:57:30

问题


Suppose I have a template class

template <typename T>
class foo {
    T m;

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

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


回答1:


You just slap a constexpr on it:

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

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]/6.



来源:https://stackoverflow.com/questions/41517603/conditionally-constexpr-member-function

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!