https://gcc.gnu.org/bugzilla/show_bug.cgi?id=85466

--- Comment #9 from Jonathan Wakely <redi at gcc dot gnu.org> ---
This is undefined:

template <typename T>
inline auto when_greater_then(T x, T y) -> decltype(std::max(sign(x - y),
T(0)))  {
  return std::max(sign(x - y), T(0));
}


The return type of std::max is a reference to one of its arguments, so the
decltype expression is a reference, and you end up returning a reference to a
local temporary.

This would fix that problem:

template <typename T>
inline auto when_greater_then(T x, T y)
-> std::remove_reference_t<decltype(std::max(sign(x - y), T(0)))>  {
  return std::max(sign(x - y), T(0));
}

But it's dumb anyway, because the return type of std::max<T> is T& so you don't
need to use decltype and remove_reference to know the type.

template <typename T>
inline T when_greater_then(T x, T y)
{
  return std::max(sign(x - y), T(0));
}

Much simpler, no dangling references.

Reply via email to