请输入您要查询的百科知识:

 

词条 Functional (C++)
释义

  1. Adaptable function objects

  2. Predefined function objects

  3. Examples

  4. References

  5. External links

{{C++ Standard library}}{{cleanup|date=December 2010}}

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|bool}} is called a predicate, and a binary function whose return type is {{Cpp|bool}} 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|operator()}} 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 {{Cpp|typedef}}s so that those types can be named and used in programs. If a type {{Cpp|F0}} is a model of an adaptable generator, then it must define {{Cpp|F0::result_type}}. Similarly, if {{Cpp|F1}} is a model of the adaptable unary function, it must define {{Cpp|F1::argument_type}} and {{Cpp|F1::result_type}}, and if {{Cpp|F2}} is a model of the adaptable binary function, it must define {{Cpp|F2::first_argument_type}}, {{Cpp|F2::second_argument_type}}, and {{Cpp|F2::result_type}}. The C++ Standard Library provides base classes {{Cpp|unary_function}} and {{Cpp|binary_function}} 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|unary_negate}} (that returns the logical complement of the value returned by a particular adaptable predicate), and {{Cpp|unary_compose}} and {{Cpp|binary_compose}}, 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|plus}}, {{Cpp|minus}}, {{Cpp|multiplies}}, {{Cpp|divides}}, {{Cpp|modulus}}, and {{Cpp|negate}}), comparisons ({{Cpp|equal_to}}, {{Cpp|not_equal_to}}, {{Cpp|greater}}, {{Cpp|less}}, {{Cpp|greater_equal}}, and {{Cpp|less_equal}}), and logical operations ({{Cpp|logical_and}}, {{Cpp|logical_or}}, and {{Cpp|logical_not}}).[1]

Examples

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

  1. include
  2. include

/* Define a template function */

template void printValue(T value)

{

}

int main(void)

{
        /* A function wrapper to a function */        std::function funcA = printValue;        funcA(2015);
        /* A function wrapper to a function pointer */        std::function funcB = &printValue;        funcB(2016);
        /* A function wapper to a lambda function. */        std::function funcC = [](int value) { std::cout << value << std::endl; };        funcC(2017);
        /* A function wrapper generated by std::bind().         * Pass a pre-defined parameter when binding.         */        std::function funcD = std::bind(printValue, "PI is");        funcD();
        /* A function wrapper generated by std::bind().         * Pass a parameter when calling the function.         */        std::function funcE = std::bind(printValue, std::placeholders::_1);        funcE(3.14159);

}

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

  1. include
  2. include

template class CAnyData {

public:

        T m_value;        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; }

};

int main(void)

{
        /* A function wrapper to a member variable of a class */        CAnyData dataA { 2016 };        std::function &)> funcA = &CAnyData::m_value;        std::cout << funcA(dataA) << std::endl;
        /* A function wrapper to member function without parameter passing */        CAnyData dataB { 2016.1 };        std::function &)> funcB = &CAnyData::print;        funcB(dataB);
        /* A function wrapper to member function with passing a parameter */        std::function &, float)> funcC = &CAnyData::printAfterAdd;        funcC(dataB, 0.1);
        /* A function wrapper to member function generated by std::bind */        std::function funcD = std::bind(&CAnyData::printAfterAdd, &dataB, std::placeholders::_1);        funcD(0.2);

}

References

1. ^{{Cite book |first=Nicolai M. |last=Josuttis |title=The C++ Standard Library |publisher=Addison-Wesley |year=1999 |isbn=978-0-201-37926-6 }}

External links

  • C++ reference for standard function objects

3 : C++|C++ Standard Library|Articles with example C++ code

随便看

 

开放百科全书收录14589846条英语、德语、日语等多语种百科知识,基本涵盖了大多数领域的百科知识,是一部内容自由、开放的电子版国际百科全书。

 

Copyright © 2023 OENC.NET All Rights Reserved
京ICP备2021023879号 更新时间:2024/9/28 17:23:32