9

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?

T.C.
  • 129,563
  • 16
  • 274
  • 404
SU3
  • 4,760
  • 2
  • 32
  • 60

1 Answers1

12

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

HTNW
  • 25,442
  • 1
  • 29
  • 56
T.C.
  • 129,563
  • 16
  • 274
  • 404