Английская Википедия:Functional (C++)

Материал из Онлайн справочника
Перейти к навигацииПерейти к поиску

Шаблон:C++ Standard library

In the context of the programming language C++, functional refers to a header file that is part of the C++ Standard Library and provides a set of predefined class templates for function objects, including operations for arithmetic, comparisons, and logic. Instances of these class templates are C++ classes that define a function call operator, and the instances of these classes can be called as if they were functions.[1] It is possible to perform very sophisticated operations without writing a new function object, simply by combining predefined function objects and function object adaptors.

The class template std::function provided by C++11 is a general-purpose polymorphic function wrapper. Instances of std::function can store, copy, and invoke any callable target—functions, lambda expressions (expressions defining anonymous functions), bind expressions (instances of function adapters that transform functions to other functions of smaller arity by providing values for some of the arguments), or other function objects.

The algorithms provided by the C++ Standard Library do not require function objects of more than two arguments. Function objects that return Boolean values are an important special case. A unary function whose return type is Шаблон:Cpp is called a predicate, and a binary function whose return type is Шаблон:Cpp is called a binary predicate.

Adaptable function objects

In general, a function object has restrictions on the type of its argument. The type restrictions need not be simple, though: Шаблон:Cpp may be overloaded or may be a member template. Similarly, there need be no way for a program to determine what those restrictions are. An adaptable function object, however, does specify what the argument and return types are, and provides nested [[typedef|Шаблон:Cpp]]s so that those types can be named and used in programs. If a type Шаблон:Cpp is a model of an adaptable generator, then it must define Шаблон:Cpp. Similarly, if Шаблон:Cpp is a model of the adaptable unary function, it must define Шаблон:Cpp and Шаблон:Cpp, and if Шаблон:Cpp is a model of the adaptable binary function, it must define Шаблон:Cpp, Шаблон:Cpp, and Шаблон:Cpp. The C++ Standard Library provides base classes Шаблон:Cpp and Шаблон:Cpp to simplify the definition of adaptable unary functions and adaptable binary functions.

Adaptable function objects are important, because they can be used by function object adaptors: function objects that transform or manipulate other function objects. The C++ Standard Library provides many different function object adaptors, including Шаблон:Cpp (that returns the logical complement of the value returned by a particular adaptable predicate), and Шаблон:Cpp and Шаблон:Cpp, which perform composition of function object.

Predefined function objects

The C++ Standard Library includes in the header file functional many different predefined function objects, including arithmetic operations (Шаблон:Cpp, Шаблон:Cpp, Шаблон:Cpp, Шаблон:Cpp, Шаблон:Cpp, and Шаблон:Cpp), comparisons (Шаблон:Cpp, Шаблон:Cpp, Шаблон:Cpp, Шаблон:Cpp, Шаблон:Cpp, and Шаблон:Cpp), and logical operations (Шаблон:Cpp, Шаблон:Cpp, and Шаблон:Cpp).[1]

Examples

Function wrappers can be used to make calls to ordinary functions or to functions objects created by lambda expressions.

#include <iostream>
#include <functional>

/* Define a template function */
template <typename T>
void PrintValue(T value) {
  std::cout << value << std::endl;
}

int main(void) {
  /* A function wrapper to a function */
  std::function<void(int)> func_a = PrintValue<int>;
  func_a(2015);

  /* A function wrapper to a function pointer */
  std::function<void(int)> func_b = &PrintValue<int>;
  func_b(2016);

  /* A function wrapper to a lambda function. */
  std::function<void(int)> func_c = [](int value) {
    std::cout << value << std::endl;
  };
  func_c(2017);

  /* A function wrapper generated by std::bind().
   * Pass a pre-defined parameter when binding.
   */
  std::function<void(void)> func_d = std::bind(PrintValue<std::string>, "PI is");
  func_d();

  /* A function wrapper generated by std::bind().
   * Pass a parameter when calling the function.
   */
  std::function<void(float)> func_e =
      std::bind(PrintValue<float>, std::placeholders::_1);
  func_e(3.14159);
}

Function wrappers also can be used to access member variables and member functions of classes.

#include <iostream>
#include <functional>

template <typename T>
class CAnyData {
 public:
  CAnyData(T value) : m_value{value} {}
  void Print(void) { std::cout << m_value << std::endl; }
  void PrintAfterAdd(T value) { std::cout << (m_value + value) << std::endl; }

  T m_value;
};

int main() {
  /* A function wrapper to a member variable of a class */
  CAnyData<int> data_a{2016};
  std::function<int(CAnyData<int> &)> func_a = &CAnyData<int>::m_value;
  std::cout << func_a(data_a) << std::endl;

  /* A function wrapper to member function without parameter passing */
  CAnyData<float> data_b{2016.1};
  std::function<void(CAnyData<float> &)> func_b = &CAnyData<float>::Print;
  func_b(data_b);

  /* A function wrapper to member function with passing a parameter */
  std::function<void(CAnyData<float> &, float)> func_c =
      &CAnyData<float>::PrintAfterAdd;
  func_c(data_b, 0.1);

  /* A function wrapper to member function generated by std::bind */
  std::function<void(float)> func_d = std::bind(&CAnyData<float>::PrintAfterAdd,
                                                &data_b, std::placeholders::_1);
  func_d(0.2);
}

References

Шаблон:Reflist

External links