Application of function functor in STL

Source: Internet
Author: User

Application of function functor in STL

Generic Type in STLAlgorithmThere are many functor applications.

 
Template <typename T> struct plus {t operator () (const T & X, const T & Y) {return x + y ;}}; template <typename T> struct minus {t operator () (const T & X, const T & Y) {return x-y ;}}; void test () {plus <int> plusobj; minus <int> minusobj; cout <plusobj (32, 45) <Endl; cout <minusobj (32, 45) <Endl; cout <plus <int> () (32, 45) <Endl; cout <minus <int> () (32, 45) <Endl ;}

In generic algorithms, many applications are followed by "anonymous objects", because in many algorithm systems, the life cycle of anonymous objects is in the algorithm, and anonymous objects are destroyed after algorithms are generated.

Example:

 
Inner_product (IV. Begin (), IV. End (), IV. Begin (), 10, minus <int> (), plus <int> ())
 
Adjacent_difference (IV. Begin (), IV. End (), oite, plus <int> ());

All passed are anonymous objects of functor.

Functor. To put it bluntly, it is a heavy load on operator.

Q: What are the symbols that cannot be overloaded in C ++?

Most operators can beProgramStaff heavy load. The exception is (Four):

. (DOT symbol )::? : Sizeof

(1) theoretically, the. (vertex operator) can be reloaded using the same technology as->. However, this will lead to a problem, that is, the operation is not sure whether the objects of. Are reloaded, or an object referenced. For example:

Class y {public: void F ();//...}; class X {// suppose you can reload. y * P; Y & operator. () {return * P;} void F ();//...}; void g (x & X) {X. F (); // X: f or y: f or error? }

This problem can be solved in several different ways. Which of the following methods is the best method for standardization.

(2) In N: M, neither n nor M is a value expression; N and m are the names known by the compiler: execute(During the compilation period), instead of evaluating the expression. As you can imagine, if X: Y is allowed to be overloaded, X may be an object rather than a namespace or a class, this will lead to the generation of a new syntax (expression 1: expression 2), which is opposite to the original expression ). Obviously, this complexity will not bring any benefits.

(3) Is there any fundamental reason to prohibit heavy loading? :. It's just because I didn't find any special case that I need to overload a ternary operator. Note that expression 1 is overloaded? Expression 2: a function of expression 3 cannot guarantee that only one of expression 2: expression 3 will be executed.

(4) sizeof cannot be reloaded because built-in operations, such as incremental operations on a pointer to an array, must be relied on. Consider:

 
X a [10]; x * P = & A [3]; x * q = & A [3]; P ++; // P points to a [4], the integer value of P must be a sizeof (x) larger than the integer value of Q)

Therefore, sizeof (x) cannot be used by programmers to give a different new meaning, so as not to violate the basic syntax.

For more details, see 《Design and evolution of c ++ Language.

Bjarne stroustrup FAQ:

Http://www2.research.att.com /~ BS/bs_faq.html



Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.