rainit2006 / C-Program

C# Program knowledge
0 stars 0 forks source link

Modern C++ #34

Open rainit2006 opened 5 years ago

rainit2006 commented 5 years ago

rainit2006 commented 5 years ago

rainit2006 commented 5 years ago
rainit2006 commented 5 years ago
rainit2006 commented 5 years ago

从 C++11 开始,被弃用的主要特性: https://changkun.de/modern-cpp/book/01-intro/index.html 注意:弃用并非彻底不能用,只是用于暗示程序员这些特性将从未来的标准中消失,应该尽量避免使用。但是,已弃用的特性依然是标准库的一部分,并且出于兼容性的考虑,大部分特性其实会『永久』保留。

rainit2006 commented 5 years ago

// C++11 auto i = begin(m); auto const xlimit = config["xlimit"]; auto& s = singleton::instance();


- 使用shared_ptr明确共享所有权。更喜欢使用make_shared来有效的创建共享对象。

// C++98 widget* pw = new widget(); ... delete pw;

// C++11 auto pw = make_shared();


- nullptr
始终使用nullptr作为空指针的值,绝不使用模棱两可的字面量0或者NULL宏,因为它们可以是一个整形或指针。

// C++98 int* p = 0;

// C++11 int* p = nullptr;


- 初始化

int n {4}; string s{"Hello"} vector values{1, 2, 3} map<string, string> Capital { {"UK", "London"}, {"China", "Beijing"} }

rainit2006 commented 5 years ago

C++ 11