Traditional Thread

C++ 11 supports multi-threading, for which we previously used pthread or boost. Standard C++ 11’s threading is based on boost::thread, now it’s cross platform and no dependency needed.

#include <thread>
#include <iostream>
int testThread()
{
	std::cout << "I am a seperate thread...";
}
void main()
{
	std::thread newThread(testThread);//now it's running
        //do something else .... //
	newThread.join();//before the ending, newThread should join
        return;
}

The code is simple, the thread function will run right after the std::thread is declared.

Continue Reading ...

Function binding

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

void originalFunc(std::string a, std::string b, std::string c)
{
    std::cout << a << " " << b << " " << c << std::endl;
}//original function, to be binded

void main()
{
  	//Newly binded function, created by bind function
  	auto newlyBindedFunc = bind(originalFunc, placeholders::_3, "Fixed", placeholders::_1);
    //Call the newly binded function
    newlyBindedFunc("New 1st",    "New 2nd",   "New 3rd");
}
Continue Reading ...

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!";
};
lam1();

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:

Continue Reading ...

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:

Continue Reading ...

You should include when using smart pointers

unique_ptr

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.

Continue Reading ...

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

Continue Reading ...

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:

Continue Reading ...

DataBinding and MVVM Pattern

When dealing with GUI, usually we will use the MVC pattern, which means we manage a set of data as Model, the model will be demonstrated as View and the Contoller controls the logic of showing the data:

mvc_role_diagram

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:

Continue Reading ...

decltype用法:

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;
}
Continue Reading ...

The type deduction of Template and auto

The type deduction of template will ignore one level of reference and pointer

Example 1:

Input argument as reference

template<typename T>
void f(T& param); 

int x = 27; // x is an int
const int cx = x; // cx is a const int
const int& rx = x; // rx is a reference to x as a const int 
f(x);// T is int
f(cx);// T is const int, param's typeis const int&
f(rx);// T is const int, param's type is const int& 
Continue Reading ...

[DllImport("user32.dll")]   
public static extern void SwitchToThisWindow(IntPtr hWnd,bool turnon);
String ProcWindow = "wechat";
private void switchToWechart()
{
    Process[] procs = Process.GetProcessesByName(ProcWindow);
    foreach (Process proc in procs)
    {
         //switch to process by name
         SwitchToThisWindow(proc.MainWindowHandle, true);
    }
}

安装基本软件

  • 首先安装一个能在windows环境下运行的包管理器Chocolatey

  • 因为Jekyll是用Ruby写的,所以要安装Ruby,在控制台中输入choco install ruby -y回车

  • 关闭控制台,然后再打开控制台并输入gem install jekyll,这样Jekyll就装好了:如果出现ssl3错误按照以下步骤(点我看原文)解决:

    在 https://rubygems.org/pages/download 下载最新版的rubygem

    cmd输入 gem install –local C:\rubygems-update-x.x.xx.gem:local后面即刚下载好的gem文件

    然后输入update_rubygems –no-ri –no-rdoc

    结束后再输入gem install jekyll,应该就可以了

  • 重新打开控制台,输入chcp 65001避免编码问题

  • 安装Ruby开发环境,在控制台中输入:

    choco install ruby2.devkit

  • C:\tools\DevKit2文件夹中打开控制台,执行命令 ruby dk.rb init,产生config.yml文件

Continue Reading ...

框架的文件夹结构

_includes :存放了一些定制的网页元素,比如header.html是整个页面的头,也就是最上面的菜单栏。又如author.html,是作者页面,用于展示作者信息。通用的JS文件都放在scripts.html里。

_layout :主要定义了两种类型页面的排版,post是为单篇文章设计的排版,post-index是为一系列文章设计的排版。

_posts:用于存放所有文章的md文件,md文件的命名必须严格按照”年-月-日-标题”的格式命名。

_sass:用于存放定制的css文件,比如_page就规定了页面各个元素的宽度颜色字体,_variables定义了一些全局变量的值。

_site:模板编译完成后生成的页面,这个是真正可以直接部署的页面,平时不用看

_templates:规定了不同类型的排版文件中可以定义的变量

前面不带下划线的文件夹存放用户自己定制的页面,比较重要的有:

images:用于存放图片

search:用于存放搜索框页面

tags:用于存放按照tags列出所有文章的页面

categories:用于存放按照category列出所有文章的页面

posts:用于存放列出所有文章的页面

Continue Reading ...