Saturday, June 14, 2014

C++ 11 study notes(2/xx) simple lambda introduction

Lambda
minimum form of lambda in C++11 is like this:

#include
using namespace std;
int main() {
[](){};
return 0;
}
and it compiles. It is just a definition of minimum lambda function.
Minimum lambda function call is like this:

[](){}();

explanation of every element is below.
first "[]" is called lambda introducer. I will write usage at the end of this entry.
second () is function arguments.
third {} is function body.
last () is lambda function call.

I will introduce simple examples below.

1. hello world in lambda is like this:

[]{std::cout << "hello lambda" << std::endl;}();
it will output string "hello lambda".

2. lambda function can be assigned to variable;

auto func = []{cout << "assign to func" << endl;};
func();

3. lambda function can be passed to another function as argument.

template
void f(Func func) {
  func();
}
f([]{cout << "pass to function as argument" << endl;});

4. passing argument to lambda function

[](string const& str){cout << str << endl;}("hah");

5. declare return type of lambda function implicitly.

  auto b = []()->float { return 3.14;}();
float is return type.

6. if we want to access local names we use lambda introducer [].

string x = "I am local string which is outside of lambda function scope";
[&](string const& str){ x += str;}(". reference capture can change value of local variable");
output will be "I am local string which is outside of lambda function scope. . reference capture can change value of local variable"
copy capture is [=].
we can specify which variable is captured by reference or copy.

int x,y,z,n;
[&, x, y]{};  //only a and b is copy capture. the others are captured by reference.
[=, &x]{};   // only x is captured by reference. the others are captured by copy.

And there was tons of features. 



  




           

C++11 study notes(1/xx)

during reading the book, I realized that I have to keep keywords and concepts in mind to continue to read book. And here is some impotant keywords from chapter6.

Chapter 6 Types and Declarations
Type category names.
Boolean, character, integer types are called integral types.
integral types and floating-point types are collectively called arithmetic types.
Enumerations + class are called user-defined types
fundamental types, pointers, references are called built-in types.

Initialization 
There are four ways to initialize a variable.
X a1{v};
X a2 = {v};
X a3 = v;
X a4(v);
The first one is new in C++11. This one do not perform the type conversion(narrowing?). and checked in compile time. Bjarne recommends this one. The other three ways are old.

classification of objects based on their lifetimes:
1. Automatic
an object declared in a function is created when its definition is encountered and destroyed when its name goes out of scope.
2. Static
Objects declared in global or namespace scope and statics declared in functions or classes are and initialized once(only) and "live" until program terminates
3. Free Store
Objects created and deleted by using the new and delete operators.
4. Temporary objects (i will complete this later)
5. Thread-local objects. (i will complete this later)

and last thing from this chapter is type alias.
in C++ there was a keyword "using" to alias the type.

1:  template<class T>  
2:  class vector {  
3:    using value_type = T;  
4:    ...  
5:  };