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

            Bug ID: 101203
           Summary: Remove unnecessary empty check in std::function
           Product: gcc
           Version: unknown
            Status: UNCONFIRMED
          Severity: normal
          Priority: P3
         Component: libstdc++
          Assignee: unassigned at gcc dot gnu.org
          Reporter: lutztonineubert at gmail dot com
  Target Milestone: ---

Hello,

this empty() check in std_function.h:

https://github.com/gcc-mirror/gcc/blob/master/libstdc++-v3/include/bits/std_function.h#L558

      _Res
      operator()(_ArgTypes... __args) const
      {
        if (_M_empty())
          __throw_bad_function_call();
        return _M_invoker(_M_functor, std::forward<_ArgTypes>(__args)...);
      }

can, as I assume, be removed to gain better performance.

An empty, default constructed std::function could be initialized with
__throw_bad_function_call() instead with nullptr/zeros.

If std::function::operator()(...) gets called, still
__throw_bad_function_call() would get called as before.

The implementation of `explicit operator bool()` or better `_M_empty()` would
then check if the function is initialized with the address of
__throw_bad_function_call.

This would be a similar technic as used in std::any:
https://github.com/gcc-mirror/gcc/blob/master/libstdc++-v3/include/experimental/any#L436

Reply via email to