Function object and Lambda

Source: Internet
Author: User

This article shows two basic concepts: Function objects and lambda. Function objects are the basis for building Nana.

Function objects (function objects or functor in English) are class objects that can be called as functions. Generally, they are class objects that define the function call Operator. Function objects are a very good technique, which is more common than common functions, because they can maintain the state after calling and can initialize and detect a single object, it is difficult to implement this through static local variables.

Class sum {public: sum (): I _ (0) {} operator int () const volatile {return I _;} // enable the class object to call void operator () (int x) Volatile {I _ + = x;} PRIVATE: int I _;} like a function _;}; void Foo (const STD: vector <int> & V) {// sum of all elements STD: cout <STD: for_each (v. begin (), V. end (), sum () <STD: Endl ;}

Function objects remain in their own State, so they can be conveniently used in parallel processing and are widely used in library implementation for good flexibility.

Nana C ++ library uses a large number of function objects to run the framework. Nana C ++ library introduces a general function object class template:

template<typename Ftype>class functor;

The template parameter FTYPE is specified as the function type that requires proxy. The class template functor frees Nana from being entangled with various types. For example:

#include <nana/gui/wvl.hpp>#include <iostream>void foo(){std::system("cls");std::cout<<"foo"<<std::endl;}void foo_with_eventinfo(const nana::gui::eventinfo& ei){std::cout<<"foo_with_eventinfo, mouse pos = ("<<ei.mouse.x<<", "<<ei.mouse.y<<")"<<std::endl;}class click_stat{public:click_stat(): n_(0){}void respond(){std::cout<<"click_stat = "<<++n_<<std::endl;}void respond_ei(const nana::gui::eventinfo& ei){std::cout<<"click_state width eventinfo = "<<n_<<", mouse pos = ("<<ei.mouse.x<<", "<<ei.mouse.y<<")"<<std::endl;}private:int n_;};int main(){using namespace nana::gui;typedef nana::functor<void()> fun_t;typedef nana::functor<void(const eventinfo&)> fun_with_param_t;form fm;click_stat cs;fun_t f(foo);fm.make_event<events::click>(f);f = fun_t(cs, &click_stat::respond);fm.make_event<events::click>(f);fun_with_param_t fp(foo_with_eventinfo);fm.make_event<events::click>(fp);fp = fun_with_param_t(cs, &click_stat::respond_ei);fm.make_event<events::click>(fp);fm.show();exec();}

There are four types of event processing functions (for event interpretation, see the article Hello Nana C ++ library or programming with Nana C ++ Library), which are uniformly processed by the functor class template. Functor templates improve flexibility and reduce learning and usage complexity.
Fig 1.3Method registration and response to various methods for clicking an event

Predefined function objects

Nana C ++ library contains some different predefined function objects. Using these function objects with template functions not only increases code readability, but also greatly improves development efficiency. For example, if a C ++ program wants to close the window when it clicks. Form. make_event <events: Click> (destroy (form); before using these function objects, include <Nana/GUI/functional. HPP>

class destroy{public:destroy(nana::gui::window wd);void operator()() const;};

Destruction window.

class hide{public:hide(nana::gui::window wd);void operator()() const;};

Hide the window.

class show{public:show(nana::gui::window wd);void operator()() const;};

Display window.

Lambda expressions

Lambda expressions are a mechanism for developing function objects. They are new features recently introduced in C ++ and are mainly used to develop simple operations for some functions. For example:

#include <nana/gui/wvl.hpp>#include <iostream>int main(){using namespace nana::gui;form fm;fm.make_event<events::click>([]{ std::cout<<"form is clicked"<<std::endl; });fm.show();exec();}

This real Parameter[] {STD: cout <"form is clicked" <STD: Endl ;}In C ++ (11), it is a "Lambda" (or "Lambda function" or "Lambda expression "). Lambda starts with a simple brackets [] and defines a function body with the compound statement block {}. In fact, Lambda defines an anonymous function object, therefore, you can use the function call syntax to call this Lambda. For example:

[]{ std::cout<<"form is clicked"<<std::endl; }();

Lambda is used to create an anonymous function object, so you can also specify a parameter list for it. For example:

fm.make_event<events::click>([](const eventinfo& ei){std::cout<<"mouse pos=("<<ei.mouse.x<<", "<<ei.mouse.y<<")"<<std::endl;});

Lambda-declarator () is used in the same way as the parameter list. Now we will suspend the introduction of Lambda. For more details, see C ++.

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.