C + + Learning Note (i) mutable function

Source: Internet
Author: User

Today look at the code in the process, see a keyword mutable

Here's a summary: mutable: Mutable is the opposite of the const action.

Const emphasis immutable (limit for breaking the const)

There's a mutable. Always in a mutable state (even if the variable is in the const function)

function

By std::function the encapsulation of various callable entities (ordinary functions, lambda expressions, function pointers, and other function objects) in C + +, a new callable Std::function object is formed; let's not dwell on so many callable entities.

The use of function is reproduced from another blog http://blog.csdn.net/hanbingfengying/article/details/28651507.

1#include <functional>2#include <iostream>3#include <map>4 using namespacestd;5 6 intAddintAintb) {returnA +b;}7Auto mod = [] (intAintb) {returnAb;};/ /LAMBDA-expression8 structDivide {9     int operator()(intAintb) {Ten         returnAb; One     } A }; -  -function<int(int,int) > func1 =add; thefunction<int(int,int) > Func2 =MoD; -function<int(int,int) >func3 =divide (); -  -map<string, function<int(int,int) > >funs = { +{"+", add}, -{"/", Divide ()}, +{"*",[](intAintb) {returnAb;}}, A{"%", mod} at }; - intMain () { -cout << func1 (1,2) <<Endl; -cout << Func2 (1,2) <<Endl; -cout << func3 (1,2) <<Endl; -cout << funs["+"](4,9) <<Endl; in     //cout << << Endl; -System"Pause"); to}

1 classCADD2 {3  Public:4CADD (): M_nsum (0) {NULL;}5     int operator()(inti)6     {7M_nsum + =i;8         returnm_nsum;9     }Ten      One     intSum ()Const A     { -         returnm_nsum; -     } the      - Private: -     intm_nsum; - }; + intMainintargcConst Char*argv[]) - { + CAdd CAdd; Afunction<int(int) > FUNCADD1 =CAdd; atfunction<int(int) > FUNCADD2 =CAdd; -COUT&LT;&LT;FUNCADD1 (Ten) <<Endl; -COUT&LT;&LT;FUNCADD2 (Ten) <<Endl; -Cout<<cadd.sum () <<Endl; -      -      return 0; in}

Output

The above output is 10 10 0. We assign the same function object to two functions, then call these two functions separately, but the value of the member variable in the function is not saved, where is the problem? Because the default behavior of a function is to copy an object that is passed to it, a copy of the Cadd object is stored in the F1,F2.

C++11 provides the ref and CREF functions to provide references to objects and frequently-referenced wrappers. If function can correctly save the state of the functions object, it can be modified as follows

1 classCADD2 {3  Public:4CADD (): M_nsum (0) {NULL;}5     int operator()(inti)6     {7M_nsum + =i;8         returnm_nsum;9     }Ten  One     intSum ()Const A     { -         returnm_nsum; -     } the  - Private: -     intm_nsum; - }; + intMainintargcConst Char*argv[]) - { + CAdd CAdd; Afunction<int(int) > FUNCADD1 =ref(CADD); atfunction<int(int) > FUNCADD2 =ref(CADD); -cout << FUNCADD1 (Ten) <<Endl; -cout << FUNCADD2 (Ten) <<Endl; -cout << cadd.sum () <<Endl; -System"Pause"); -     return 0; in}

In addition, when a value is assigned between two functions, the target function holds a copy of the function object if the source function holds a copy of the function object. If the source function holds a reference to a function object, the target function holds a reference to the functional object.

Make a summary: http://blog.csdn.net/wangshubo1989/article/details/49134235

The class template std::function is a general-purpose, polymorphic function encapsulation. An instance of Std::function can store, copy, and invoke operations on any target entity that can be called, including ordinary functions, lambda expressions, function pointers, and other function objects. The Std::function object is a type-safe package for existing callable entities in C + + (we know that callable entities such as function pointers are unsafe types).

Usually Std::function is a function object class that wraps any other function object, the wrapped function object has n parameters of type T1, ..., TN, and returns a value that can be converted to type R. Std::function uses the template transformation constructor to receive the wrapped function object, in particular, the closure type can be implicitly converted to std::function.

The simplest way to understand this is:

By std::function the encapsulation of various callable entities (ordinary functions, lambda expressions, function pointers, and other function objects) in C + +, a new callable Std::function object is formed; let's not dwell on so many callable entities.

Reference: http://blog.csdn.net/xf_zhen/article/details/52224139

C + + Learning Note (i) mutable function

Related Article

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.