Function binding

std::bind can create a function which is binded to another one:

void originalFunc(int a, double b, string c)
	cout << a << " " << b << " " << c.c_str() << endl;
}//original function, to be binded

void main()
  	//Newly binded function, created by bind function
  	auto newlyBindedFunc = bind(originalFunc, 
	newlyBindedFunc("Hello world",2);//call the new func
        originalFunc(2,10.5,"Hello world");//the same result

lambda expression can be understood as an anonymous function, it doesn’t need a name, it is created in runtime:

auto lam1 = []()
  cout<<"Hello World!";

The code above shows a simplest lambda expression, when lam1() is called, it will print “Hello World!”.

How is the lambda expression existed in C++?

It is existed a closure class, each lambda is a unique closer class generated by the compiler in runtime. So it is a object and can be copied:

shared_ptr is a pointer allowed to be copied, it is like the normal pointer we use, but it’s smart for its counting function.

A normal pointer is not safe as when we are deleting it, we don’t know if someone else is using it. The shared_ptr count the users of this pointer, when someone release the pointer, its count will decrement, the memory block where it points will release only when the count becomes 0, which means no one is using it.

Initialize a shared_ptr:

shared_ptr<int> s{ new int{3} };

It can also be “made”, even made directly from value:

You should include when using smart pointers


Just as its name means, unique_ptr guarantees that there is only one pointer point to a specific memory block. So it cannot be copied by only moved.

Init a unique_ptr

unique_ptr<int> p{ new int };
unique_ptr<int> p{ new int{4} };

As you can see, smart pointers are wraps of normal pointers, we call them “raw” pointers.

constexpr is more strict than const

int a;
const b = a;//correct
constexpr c = a;//wrong, because a has no value yet

When declaring a constexpr, it should be assigned with a already existed value.

you can get compile-time result when using constexpr function

Alias is preferred to typedef

It is clearer:

typedef void (*FP)(int, const std::string&); // old way: typedef
using FP = void (*)(int, const std::string&); // new way: alias

It support template:

template<typename T>
using MyAllocList = std::list<T, MyAlloc<T>>;

enum is preferred to be scoped

Comparison between the old and new way:

DataBinding and MVVM Pattern

When dealing with GUI, normally we use the so-called MVC pattern, which means we manager a set of data as Model and we demonstrate them as View, the Contoller controls the logic of showing the data:


The main problem of this pattern is that most of the controller’s works are simple “get” and “set” functions between the data and the gui Like:


In C++11, perhaps the primary use for decltypeis declaring function templates where the function’s return typedepends on its parameter types.

decltype 用于获得某个未知变量的类型,在什么情况下我们不知道变量的类型呢? 当然是使用template或auto的时候:

template<typename T>
void funcValue(T param)
	auto subParam = param;
	decltype(auto) subSubParam = subParam;