SlideShare uma empresa Scribd logo
1 de 39
Baixar para ler offline
C++ 11
潘旻琦·孝达
学习C/C++的意义
• TIOBE 稳定全球排名第⼀一、第四的编程语⾔言
• 接触底层,获得更⼤大的技术提升空间
• 业余时间向⼀一些⽜牛逼的项⺫⽬目提交补丁获得成就感
• ⽤用于⼚厂内开发,例如hsfcpp、酷盘服务器
四年过去了
再不学点 C++11 你就不认得 C++ 了
C++ 的进化是近⼏几年的事
1998年到2011年⼗十三年都没什么变化
* 这⼏几年导致 C++ 的进步速度远远落后于 Java
* C++ 的诸多语⾔言设计上的落后导致开发效率低下
* 进⽽而导致很多⼈人选择更易学易开发的 Java
* 很多⼈人是被逼的才⽤用C++的,例如做GUI
如果 C++ 的开发效率也能得到提⾼高
选择 C++,你将获得很多额外的好处
• 编译成原⽣生代码
• ⽆无运⾏行时虚拟机
• 亲 C 性,易整合 C 库
• 亲内存⽆无 GC,省内存
• 快!
放⼼心使⽤用
• gcc 4.8 以后已经完全⽀支持C++11,最新4.9.2
• clang 3.x 已经完全⽀支持C++11,最新3.6.0
• VC 2015 Preview ⼏几乎已经完全⽀支持C++11
• kanbox-web-pub.cm3 ⺫⽬目前不⾏行,gcc才4.1
• 10.101.86.86⺫⽬目前不⾏行,同上
推荐使⽤用clang与libc++
推荐使⽤用clang与libc++
auto
decltype - ⽐比auto更通⽤用
返回类型后置
以前根本没法实现的⼀一种范型
有了decltype还是不⾏行,编译时parser去哪找x和y?
⽤用lambda写类似于Ruby的block式调⽤用
类似于Ruby的tap的写法
类似于Ruby的proc { }存储匿名函数
可是要把lambda传⼊入其他函数怎么办?!
不能⽤用auto,因为不能通过未知的未来调⽤用推断类型!
可以⽤用std::function模板去声明
没有GC,闭包肿么办?
总算添加了Unicode⽀支持!
u8"I'm a UTF-8 string."	
u"This is a UTF-16 string."	
U"This is a UTF-32 string."	
u8"This is a Unicode Character: u2018."	
u"This is a bigger Unicode Character: u2018."	
U"This is a Unicode Character: U00002018."	
u8R"XXX(I'm a"raw UTF-8" string.)XXX"	
uR"*(This is a "raw UTF-16" string.)*"	
UR"(This is a "raw UTF-32"string.)"
参数⻓长度可变的模板
参数⻓长度可变的函数
• 像是 printf ,以前就⼀一直在⽤用;但以前要⽤用
stdarg.h,va_start va_arg va_end
• 现在可以⽤用...操作符来做这件事
template<typename... Params> void
printf(const std::string &str_format,
Params... parameters);
通过⻓长度可变的参数实现模板递归
新增using⽤用法 - 继承构造函数!
using继承构造函数+多继承!
让编译器编译默认⽅方法!
新增=default⽤用法
让编译器不编译默认⽅方法!
新增=delete⽤用法
防⽌止覆写退化为创建新⽅方法!
新增override关键词
防⽌止继承 / 覆写!
新增final关键词
新增⼤大括号构造法
新增移动构造函数
新增的右
值引⽤用
来优化这
种代码
导致标准库的⼤大⾯面积重写
右值引⽤用
另:右值引⽤用修饰符后置
Vector⼤大⼩小变化后
以前的对象
直接移动⽽而不复制了
x2 因为重置vector⼤大⼩小
std::move⽤用于让编译器选择右值的重构版本
⼏几乎等价于static_cast<T&&>
新增关键词constexpr
解决⽆无法确定是不是常量表达式⽽而编译不了的问题
程序员必须保证constexpr的函数
或对象必须为编译时常量
新增for的另外⼀一种形式
新增nullptr!
可区分0和(void *)0进⾏行函数重载
导致标准库的⼀一些重写
某些⽅方法返回nullptr⽽而不再返回NULL
void foo(char *);	
void foo(int);	
!
char *pc = nullptr; // OK	
int *pi = nullptr; // OK	
bool b = nullptr; // OK. b is false.	
int i = nullptr; // error	
!
foo(nullptr); // calls foo(nullptr_t), not foo(int);
新增强类型枚举(enum class)
enum class Enumeration {	
Val1,	
Val2,	
Val3 = 100,	
Val4 // = 101	
};	
enum Enum1;	
// 上⾯面的不合法,没类型	
enum Enum2 : unsigned int;	
// 合法,明确表⽰示是unsigned int类型	
enum class Enum3;	
// 合法,没说,但是默认是int	
enum class Enum4 : unsigned int;	
// 合法	
enum Enum2 : unsigned short;	
// 不合法,已经声明过是int怎么⼜又变成short了?!
新增关键字static_assert!
⽤用于编译时排错
static_assert((GREEKPI > 3.14) && (GREEKPI < 3.15), "GREEKPI is
inaccurate!”);	
!
//类级:	
template<class T>	
struct Check {	
static_assert(sizeof(int) <= sizeof(T), "T is not big enough!");	
};	
!
//⽅方法级:	
template<class Integral>	
Integral foo(Integral x, Integral y) {	
static_assert(std::is_integral<Integral>::value, "foo() parameter must
be an integral type.");	
}
⾃自定义字⾯面值后缀
OutputType operator "" _suffix(const char * literal_string);	
!
OutputType some_variable = 1234_suffix;	
!
template<char...> OutputType operator "" _tuffix();	
!
OutputType some_variable = 1234_tuffix;	
// operator "" _tuffix<'1', '2', '3', '4'>()	
OutputType another_variable = 2.17_tuffix;
新增thread_local类似于ruby的Thread.current[]
下⼀一步:C++ 14
• clang 3.4 已经全部⽀支持

Mais conteúdo relacionado

Destaque

How Race, Age and Gender Shape Attitudes Towards Mental Health
How Race, Age and Gender Shape Attitudes Towards Mental HealthHow Race, Age and Gender Shape Attitudes Towards Mental Health
How Race, Age and Gender Shape Attitudes Towards Mental Health
ThinkNow
 
Social Media Marketing Trends 2024 // The Global Indie Insights
Social Media Marketing Trends 2024 // The Global Indie InsightsSocial Media Marketing Trends 2024 // The Global Indie Insights
Social Media Marketing Trends 2024 // The Global Indie Insights
Kurio // The Social Media Age(ncy)
 

Destaque (20)

2024 State of Marketing Report – by Hubspot
2024 State of Marketing Report – by Hubspot2024 State of Marketing Report – by Hubspot
2024 State of Marketing Report – by Hubspot
 
Everything You Need To Know About ChatGPT
Everything You Need To Know About ChatGPTEverything You Need To Know About ChatGPT
Everything You Need To Know About ChatGPT
 
Product Design Trends in 2024 | Teenage Engineerings
Product Design Trends in 2024 | Teenage EngineeringsProduct Design Trends in 2024 | Teenage Engineerings
Product Design Trends in 2024 | Teenage Engineerings
 
How Race, Age and Gender Shape Attitudes Towards Mental Health
How Race, Age and Gender Shape Attitudes Towards Mental HealthHow Race, Age and Gender Shape Attitudes Towards Mental Health
How Race, Age and Gender Shape Attitudes Towards Mental Health
 
AI Trends in Creative Operations 2024 by Artwork Flow.pdf
AI Trends in Creative Operations 2024 by Artwork Flow.pdfAI Trends in Creative Operations 2024 by Artwork Flow.pdf
AI Trends in Creative Operations 2024 by Artwork Flow.pdf
 
Skeleton Culture Code
Skeleton Culture CodeSkeleton Culture Code
Skeleton Culture Code
 
PEPSICO Presentation to CAGNY Conference Feb 2024
PEPSICO Presentation to CAGNY Conference Feb 2024PEPSICO Presentation to CAGNY Conference Feb 2024
PEPSICO Presentation to CAGNY Conference Feb 2024
 
Content Methodology: A Best Practices Report (Webinar)
Content Methodology: A Best Practices Report (Webinar)Content Methodology: A Best Practices Report (Webinar)
Content Methodology: A Best Practices Report (Webinar)
 
How to Prepare For a Successful Job Search for 2024
How to Prepare For a Successful Job Search for 2024How to Prepare For a Successful Job Search for 2024
How to Prepare For a Successful Job Search for 2024
 
Social Media Marketing Trends 2024 // The Global Indie Insights
Social Media Marketing Trends 2024 // The Global Indie InsightsSocial Media Marketing Trends 2024 // The Global Indie Insights
Social Media Marketing Trends 2024 // The Global Indie Insights
 
Trends In Paid Search: Navigating The Digital Landscape In 2024
Trends In Paid Search: Navigating The Digital Landscape In 2024Trends In Paid Search: Navigating The Digital Landscape In 2024
Trends In Paid Search: Navigating The Digital Landscape In 2024
 
5 Public speaking tips from TED - Visualized summary
5 Public speaking tips from TED - Visualized summary5 Public speaking tips from TED - Visualized summary
5 Public speaking tips from TED - Visualized summary
 
ChatGPT and the Future of Work - Clark Boyd
ChatGPT and the Future of Work - Clark Boyd ChatGPT and the Future of Work - Clark Boyd
ChatGPT and the Future of Work - Clark Boyd
 
Getting into the tech field. what next
Getting into the tech field. what next Getting into the tech field. what next
Getting into the tech field. what next
 
Google's Just Not That Into You: Understanding Core Updates & Search Intent
Google's Just Not That Into You: Understanding Core Updates & Search IntentGoogle's Just Not That Into You: Understanding Core Updates & Search Intent
Google's Just Not That Into You: Understanding Core Updates & Search Intent
 
How to have difficult conversations
How to have difficult conversations How to have difficult conversations
How to have difficult conversations
 
Introduction to Data Science
Introduction to Data ScienceIntroduction to Data Science
Introduction to Data Science
 
Time Management & Productivity - Best Practices
Time Management & Productivity -  Best PracticesTime Management & Productivity -  Best Practices
Time Management & Productivity - Best Practices
 
The six step guide to practical project management
The six step guide to practical project managementThe six step guide to practical project management
The six step guide to practical project management
 
Beginners Guide to TikTok for Search - Rachel Pearson - We are Tilt __ Bright...
Beginners Guide to TikTok for Search - Rachel Pearson - We are Tilt __ Bright...Beginners Guide to TikTok for Search - Rachel Pearson - We are Tilt __ Bright...
Beginners Guide to TikTok for Search - Rachel Pearson - We are Tilt __ Bright...
 

C++ 11