[C++笔记]基础语法

325 阅读1分钟

预编译

GNU文档:gcc.gnu.org/onlinedocs/…

MSDN文档:docs.microsoft.com/en-us/cpp/b…

预置类型

Wide character

const char *str = "str";
const wchar_t* wstr = L"str";

对象初始化

zh.cppreference.com/w/cpp/langu…

// Value initialization
std::string s{};
// Direct initialization
std::string s("hello");

拷贝构造/赋值,迁移构造/赋值

  • 迁移构造/赋值

zh.cppreference.com/w/cpp/langu…

MSDN文档:docs.microsoft.com/en-us/cpp/c…

类型推断

zh.cppreference.com/w/cpp/langu…

auto a = 1 + 2;

Lambda

zh.cppreference.com/w/cpp/langu…

int divisor = 2;
auto mod = [=, divisor](int num) -> int { return num % divisor; };
int reminder = mod(3); // reminder = 1

并发编程

Most vexing parse

// Value initialization
std::thread my_thread{ background_task() };
// Value initialization
std::thread my_thread([]{
    do_something();
    do_something_else();
});