Function objects

Header <experimental/functional> synopsis

#include <functional>

namespace std {
  namespace experimental::inline fundamentals_v3 {

    
    template<class> class function; // not defined
    template<class R, class... ArgTypes> class function<R(ArgTypes...)>;

    template<class R, class... ArgTypes>
    void swap(function<R(ArgTypes...)>&, function<R(ArgTypes...)>&);

    template<class R, class... ArgTypes>
    bool operator==(const function<R(ArgTypes...)>&, nullptr_t) noexcept;

  } // namespace experimental::inline fundamentals_v3
} // namespace std

Class template function

Overview

The specification of all declarations within subclause are the same as the corresponding declarations, as specified in , unless explicitly specified otherwise. std::experimental::function uses std::bad_function_call, there is no additional type std::experimental::bad_function_call.

namespace std {
  namespace experimental::inline fundamentals_v3 {

    template<class> class function; // undefined

    template<class R, class... ArgTypes>
    class function<R(ArgTypes...)> {
    public:
      using result_type = R;

      using allocator_type = std::pmr::polymorphic_allocator<>;

      function() noexcept;
      function(nullptr_t) noexcept;
      function(const function&);
      function(function&&);
      template<class F> function(F);
      function(allocator_arg_t, const allocator_type&) noexcept;
      function(allocator_arg_t, const allocator_type&, nullptr_t) noexcept;
      function(allocator_arg_t, const allocator_type&, const function&);
      function(allocator_arg_t, const allocator_type&, function&&);
      template<class F> function(allocator_arg_t, const allocator_type&, F);

      function& operator=(const function&);
      function& operator=(function&&);
      function& operator=(nullptr_t) noexcept;
      template<class F> function& operator=(F&&);
      template<class F> function& operator=(reference_wrapper<F>);

      ~function();

      void swap(function&);

      explicit operator bool() const noexcept;

      R operator()(ArgTypes...) const;

      const type_info& target_type() const noexcept;
      template<class T> T* target() noexcept;
      template<class T> const T* target() const noexcept;

      allocator_type get_allocator() const noexcept;
    };

    template <class R, class... ArgTypes>
      function(R(*)(ArgTypes...)) -> function<R(ArgTypes...)>;

    template<class F>
      function(F) -> function<see below>;

  } // namespace experimental::inline fundamentals_v3
} // namespace std

Construct/copy/destroy

A function object stores an allocator object of type std::pmr::polymorphic_allocator<>, which it uses to allocate memory for its internal data structures. In the function constructors, the allocator is initialized (before the target object, if any) as follows:

  • For the move constructor, the allocator is initialized from f.get_allocator(), where f is the parameter of the constructor.
  • For constructors having a first parameter of type allocator_arg_t, the allocator is initialized from the second parameter.
  • For all other constructors, the allocator is value-initialized.

In all cases, the allocator of a parameter having type function&& is unchanged. If the constructor creates a target object, that target object is initialized by uses-allocator construction with the allocator and other target object constructor arguments. If a constructor parameter of type experimental::function&& has an allocator equal to that of the object being constructed, the implementation can often transfer ownership of the target rather than constructing a new one.

The deduction guide template<class F> function(F) -> function<see below>; is specified in .

function& operator=(const function& f); function(allocator_arg, get_allocator(), f).swap(*this); *this. function& operator=(function&& f); function(allocator_arg, get_allocator(), std::move(f)).swap(*this); *this. function& operator=(nullptr_t) noexcept; If *this != nullptr, destroys the target of this. !(*this). The stored allocator is unchanged. *this. template<class F> function& operator=(F&& f); declval<decay_t<F>&>() is Lvalue-Callable () for argument types ArgTypes... and return type R. function(allocator_arg, get_allocator(), std::forward<F>(f)).swap(*this); *this. template<class F> function& operator=(reference_wrapper<F> f) noexcept; function(allocator_arg, get_allocator(), f).swap(*this); *this.

Modifiers

void swap(function& other); this->get_allocator() == other.get_allocator(). Interchanges the targets of *this and other. Nothing. The allocators of *this and other are not interchanged.

Observers

allocator_type get_allocator() const noexcept; A copy of the allocator initialized during construction () of this object.