"C ++ 11/14 Advanced Programming Boost library Quest" in Chapter 1, the new C ++ language (iii) learning record

"C ++ 11/14 Advanced Programming Boost library Quest" in Chapter 1, the new C ++ language (iii) learning record

1.7 Functional Programming

Functional programming is a process-oriented programming, object-oriented and generic programming a parallel programming paradigm, which is based on λ calculus, the calculation process as a combination of computing mathematical functions.

1.7.1 lambda expression

The basic form:

[](params){...}

[] Operator called lead lambda expression, function parameters are in parentheses, braces is a function of the body, any C ++ statements.

Type closures called lambda expressions, can not write directly, it is often necessary to use auto memory function type inference. Examples are as follows:

[root@192 C++11]# cat lambdademo.cpp
#include <iostream>
#include <vector>
#include <algorithm>
#include <string>
using namespace std;

F1 = Auto [] (int X)
{
return X * X;
}; // end expressions, statements need to end with a semicolon

auto f2 = [](string s)
{
cout<<"lambda:"<<s<<endl;
};

auto f3 = [](int x, int y)
{
return x<y;
};

int main()
{
cout<<f1(3)<<endl;
f2("lambda demo");
cout<<f3(1,5)<<endl;

vector<int> v = {1,3,5,7};
for_each(v.begin(), v.end(), [](int x){cout<<x<<",";});
for_each(v.begin(), v.end(), [](int& x){if(x>3){x *= 2;}});
return 0;
}
[root@192 C++11]# g++ lambdademo.cpp -o lambdademo -std=c++11

1.7.2 Capture external variables

Complete declaration syntax lambda expression is:

[captures](params) mutable->type {...}

captures the catch list is called, you may capture the variables outside the scope of the expression, the use of the functions that directly, which is different from the ordinary function or a function of an object at the maximum.

Capture capture list can have multiple entries separated by commas, rules are as follows:

[]: ,, no capture the functions that can not access any external variables

[=]: By value (copied) to capture all the external variables, the functions that can be accessed, but not modified

[&]: Reference capture all external variables, the function body to access and modify (for an invalid careful reference)

[Var]: by value (copied) to capture an external variable, the functions that can be accessed, but not modified

[& Var]: reference capture an external variable, the function body to access and modify

[This]: capture this pointer can access the class member variables and member functions

[=, & Var]: capturing a reference variable var, other external variables captured value

[&, Var]: Capture variable value var, references to other external variables used to capture

lambda expression can also be modified using the keyword mutable, it is captured, the value is added to an exception, allowing the variables can be modified in vivo function, but this is only copied inside, without the influence of external variables, for example:

[root@192 C++11]# cat lambda_multable_demo.cpp
#include <iostream>
#include <algorithm>
using namespace std;

main int ()
{
int X = 0;
Auto F1 = [=] () {return the mutable X ++;}; // only modifications inside, without the influence of external variables
F1 ();
COUT << "multable X:" X << endl <<;

// Auto F2 = [=] () {return X ++;}; // compile error, INCREMENT of Read-only variable 'X'
// F2 ();
// << COUT "value x:" << x << endl;

auto f3 = [&](){return ++x;};
f3();
cout<<"ref x:"<<x<<endl;
return 0;
}
[root@192 C++11]# g++ lambda_multable_demo.cpp -o lambda_multable_demo -std=c++11
[root@192 C++11]# ./lambda_multable_demo
multable x:0
ref x:1

1.8 Concurrent Programming

Thread Local Storage refers to the variable have more than one instance of the process, each thread will have a fully independent, localized copy threads, multiple threads to read and write variables without disturbing each other, completely avoid the competition, synchronization of trouble , for example:

[root@192 C++11]# cat thread_local_demo.cpp
#include <iostream>
#include <algorithm>
#include <thread>
using namespace std;

int main()
{
extern int x;
static int y = 0;
thread_local int z = 0;

auto f = [&](){++y;++z;cout<<y<<","<<z<<endl;};

thread t1(f);
thread t2(f);

t1.join ();
t1.join ();

COUT << << Y "," Z << << endl;
return 0;
}
[the root. 11 @ 192 C ++] -o thread_local_demo thread_local_demo.cpp # G ++ - C ++. 11 -lpthread = STD
[C ++ the root. 11 @ 192] # ./thread_local_demo
./thread_local_demo: /lib64/libstdc++.so.6: Version GLIBCXX_3.4.22 ` 'Not found (required by ./thread_local_demo) / / in the case of Redhat7.3 not upgraded glibc does not support thread functions, need to upgrade the version of glibc job.

The actual output is 1,1 thread t1 t2 actual thread actual output of 2,1 to 2,0 the output of the main thread

Lifecycle variable thread_local rather special, it is configured at the thread start, at the end of the thread destructor, i.e., only thread of the life cycle is effective.

thread_local to apply only where threads need isolated storage, when you need access to shared resources between threads still need to use mutex and other protection mechanisms.

1.9 security-oriented programming

1.9.1 no exception guarantee

void func () noexcept {} // function determines no exception is thrown

 1.10 Miscellaneous

In C ++ 98, the template parameter list can not have two consecutive right angle bracket >> or the compiler will interpret it as a right-shift operator, which results in a write some complex class template, template parameters It must be separated by a space more> end of the list, but make up for this flaw in C ++ 11/14, the face >>, priority will be explained in a statement in the end tag template template parameter list.

Guess you like

Origin www.cnblogs.com/snake-fly/p/12622960.html