C++11语法甜点2
语法甜点11:非成员的begin和end 在C++ 03中,标准容器都提供了begin和end成员函数,但对于普通数组,则只能使用不同的写法。比如:
1 vector<int> v; 2 int a[100]; 3 sort(v.begin(), v.end()); 4 sort(a, a+sizeof(a)/sizeof(a[0]));
为了统一语法,C++ 11提供了非成员的begin和end函数。用法如下:
1 sort(begin(v), end(v)); 2 sort(begin(a), end(a));
1 class Base 2 {3 virtual void some_func(float);4 };5 6 class Derived : public Base7 {8 virtual void some_func(int) override; // 将产生编译错误9 virtual void some_func(float) override; // 正确 10 };
1 class Base1 final { };2 class Derived1 : public Base1 { }; // 将产生编译错误3 class Base24 {5 virtual void f() final;6 };7 class Derived2 : public Base28 {9 void f(); // 将产生编译错误 10 };
enum class IColor1 { Red, Blue, Gree=100, Black };
IColor1不能隐式地转换为整数类型,也不能与整数类型比较大小。使用枚举名时,必须明确指定其所属范围,比如:必须使用IColor1::Red,而不能单独使用Red。
1 enum class IColor2 : unsigned int { Red, Blue, Gree=100, Black }; 2 enum IColor3 : unsigned int { Red, Blue, Gree=100, Black };
另外,在C++ 03中,无法对枚举类型进行前置声明。而在C++ 11中,只要是使用了指定数据类型的新式枚举,都可以进行前置声明。比如:
1 enum class IColor1; 2 enum class IColor2 : unsigned int; 3 enum IColor3 : unsigned int;
1 template< typename first, typename second, int third> 2 class SomeType; template< typename second> 3 typedef SomeType<OtherType, second, 5> TypedefName; // 在C++ 03中是不合法的
为了能够定义类模板的别名,C++ 11允许像下面这样使用using关键字:
1 template< typename first, typename second, int third> 2 class SomeType; 3 template< typename second> 4 using TypedefName = SomeType<OtherType, second, 5>;
另外,using也能定义一般类型的别名,此时等同于typedef。比如:
1 typedef void (*Func)(int); 2 using Func = void (*)(int);
1 struct point2 {3 point() {}4 point(int x, int y): m_x(x), m_y(y) {}5 int m_x, m_y;6 };7 union8 {9 int z; 10 double w; 11 point p; // 在C++ 03中不合法;在C++ 11中合法 12 };
备注:C++ 03中不适合做union成员变量的情形有以下几种:
1 u8"I'm a UTF-8 string." 2 u"This is a UTF-16 string." 3 U"This is a UTF-32 string."
1 R"(The String Data \ Stuff " )" 2 R"delimiter(The String Data \ Stuff " )delimiter"
1 u8R"XXX(I'm a "raw UTF-8" string.)XXX" 2 uR"*@(This is a "raw UTF-16" string.)*@" 3 UR"(This is a "raw UTF-32" string.)"
1 struct SomeType { OtherType member; }; 2 sizeof(SomeType::member); // 在C++ 03中不合法;在C++ 11中合法
1 int a[5] = {-2, -1, 0, 1, 2}; 2 auto funIsPositive = [](int v){return v>0;}; 3 bool bRet = all_of(a, a+5, funIsPositive); // false 4 bRet = any_of(a, a+5, funIsPositive); // true 5 bRet = none_of(a, a+5, funIsPositive); // false 6 int b[5] = {0}; 7 copy_n(a, 5, b); // 将a开始的5个元素拷贝到b中 8 copy_if(a, a+5, b, funIsPositive); // 将1, 2两个数拷贝到b中 9 iota(a, a+5, 10); // a中的每个元素加10
1 int GetFive() { return 5; } 2 int some_value[GetFive() + 5]; // 不合法
1 constexpr int GetFive() { return 5; } 2 int some_value[GetFive() + 5];
ref
得到一个包装引用 (至于常引用,则可以通过 cref
得到)。考虑下面的代码:
1 void f (int &r) { r++; } 2 template<class F, class P> void g (F f, P t) { f(t); } 3 4 int n = 0 ; 5 g(f, n) ; 6 cout << n << endl; // 輸出0 7 g(f, ref(n)); 8 cout << n << endl; // 輸出1
语法甜点11:非成员的begin和end 在C++ 03中,标准容器都提供了begin和end成员函数,但对于普通数组,则只能使用不同的写法。比如:
1 vector<int> v; 2 int a[100]; 3 sort(v.begin(), v.end()); 4 sort(a, a+sizeof(a)/sizeof(a[0]));
为了统一语法,C++ 11提供了非成员的begin和end函数。用法如下:
1 sort(begin(v), end(v)); 2 sort(begin(a), end(a));
1 class Base 2 {3 virtual void some_func(float);4 };5 6 class Derived : public Base7 {8 virtual void some_func(int) override; // 将产生编译错误9 virtual void some_func(float) override; // 正确 10 };
1 class Base1 final { };2 class Derived1 : public Base1 { }; // 将产生编译错误3 class Base24 {5 virtual void f() final;6 };7 class Derived2 : public Base28 {9 void f(); // 将产生编译错误 10 };
enum class IColor1 { Red, Blue, Gree=100, Black };
IColor1不能隐式地转换为整数类型,也不能与整数类型比较大小。使用枚举名时,必须明确指定其所属范围,比如:必须使用IColor1::Red,而不能单独使用Red。
1 enum class IColor2 : unsigned int { Red, Blue, Gree=100, Black }; 2 enum IColor3 : unsigned int { Red, Blue, Gree=100, Black };
另外,在C++ 03中,无法对枚举类型进行前置声明。而在C++ 11中,只要是使用了指定数据类型的新式枚举,都可以进行前置声明。比如:
1 enum class IColor1; 2 enum class IColor2 : unsigned int; 3 enum IColor3 : unsigned int;
1 template< typename first, typename second, int third> 2 class SomeType; template< typename second> 3 typedef SomeType<OtherType, second, 5> TypedefName; // 在C++ 03中是不合法的
为了能够定义类模板的别名,C++ 11允许像下面这样使用using关键字:
1 template< typename first, typename second, int third> 2 class SomeType; 3 template< typename second> 4 using TypedefName = SomeType<OtherType, second, 5>;
另外,using也能定义一般类型的别名,此时等同于typedef。比如:
1 typedef void (*Func)(int); 2 using Func = void (*)(int);
1 struct point2 {3 point() {}4 point(int x, int y): m_x(x), m_y(y) {}5 int m_x, m_y;6 };7 union8 {9 int z; 10 double w; 11 point p; // 在C++ 03中不合法;在C++ 11中合法 12 };
备注:C++ 03中不适合做union成员变量的情形有以下几种:
1 u8"I'm a UTF-8 string." 2 u"This is a UTF-16 string." 3 U"This is a UTF-32 string."
1 R"(The String Data \ Stuff " )" 2 R"delimiter(The String Data \ Stuff " )delimiter"
1 u8R"XXX(I'm a "raw UTF-8" string.)XXX" 2 uR"*@(This is a "raw UTF-16" string.)*@" 3 UR"(This is a "raw UTF-32" string.)"
1 struct SomeType { OtherType member; }; 2 sizeof(SomeType::member); // 在C++ 03中不合法;在C++ 11中合法
1 int a[5] = {-2, -1, 0, 1, 2}; 2 auto funIsPositive = [](int v){return v>0;}; 3 bool bRet = all_of(a, a+5, funIsPositive); // false 4 bRet = any_of(a, a+5, funIsPositive); // true 5 bRet = none_of(a, a+5, funIsPositive); // false 6 int b[5] = {0}; 7 copy_n(a, 5, b); // 将a开始的5个元素拷贝到b中 8 copy_if(a, a+5, b, funIsPositive); // 将1, 2两个数拷贝到b中 9 iota(a, a+5, 10); // a中的每个元素加10
1 int GetFive() { return 5; } 2 int some_value[GetFive() + 5]; // 不合法
1 constexpr int GetFive() { return 5; } 2 int some_value[GetFive() + 5];
ref
得到一个包装引用 (至于常引用,则可以通过 cref
得到)。考虑下面的代码:
1 void f (int &r) { r++; } 2 template<class F, class P> void g (F f, P t) { f(t); } 3 4 int n = 0 ; 5 g(f, n) ; 6 cout << n << endl; // 輸出0 7 g(f, ref(n)); 8 cout << n << endl; // 輸出1
C++11语法甜点2相关推荐
- C++ 11语法甜点2
转自:http://www.cnblogs.com/hujian/archive/2012/12/07/2807941.html 语法甜点11:非成员的begin和end 在C++ 03中,标准容器都 ...
- C++11主要新增使用语法介绍
目录 1. C++11简介 2. 统一的列表初始化 2.1 {}初始化 2.2 std::initializer_list 3. 声明 3.1 auto 3.2 decltype 3.3 nullpt ...
- JavaScript核心语法总结
1. 变量: ①先声明变量再赋值: var width; //var-用于声明变量的关键字 width = 5; //width-变量名 ②同时声明和赋值变量: var catName= " ...
- [C++11]常量表达式函数
constexpr修饰函数. 普通函数/类成员函数. 1.函数必须要有返回值,并且return返回的表达式必须是常量表达式. 代码如下: #include <iostream> using ...
- 编译C++11/14方法
Linux环境默认不支持C++11语法,通过下面方式编译 1.通过man g++可知,在g++编译选项后添加:-std=c++11即可 //编译C++11 # g++ test.cpp -std=c+ ...
- C++11更新内容(2)--完美转发--默认移动构造/移动赋值--1116
1.完美转发 1.1万能引用 void Fun(int &x){ cout << "左值引用" << endl; } void Fun(const ...
- 【C++】-- C++11基础常用知识点(下)
上篇: [C++]-- C++11基础常用知识点(上)_川入的博客-CSDN博客 目录 新的类功能 默认成员函数 可变参数模板 可变参数 可变参数模板 empalce lambda表达式 C++98中 ...
- C++ 11 内容总结
核心语言的执行期表现强化 右值引用和move语义 在C++03及之前的标准,临时对象(称为右值"R-values",因为它们通常位于赋值运算子右侧)无法被改变,在C中亦同(且被视为 ...
- 基于C++11的muduo网络库
文章目录 写在前面 项目编译问题 库安装的问题 项目测试代码 关于压力测试 项目概述 muduo网络库的reactor模型 muduo的设计 muduo各个类 辅助类 NonCopyable Time ...
最新文章
- 在C#中使用代理的方式触发事件
- Cisco实战——不让坏人登陆设备
- Windows Phone 7 Image Controller: Zoom In, Zoom Out, and Rotate (WP 7 图像控制器:放大,缩小,旋转)...
- pytorch实现简易分类模型
- MIN()与MAX()函数 的注意事项
- 第04篇 JDK版本导致Unsupported major.minor version 52.0 error
- google compute test Problem 1题
- 【转】PHP的Trait 特性
- CodeForces 757B Bash's Big Day(线性筛)
- 前端学习(2375):项目介绍结束
- c++ 协程_用yield实现协程
- NodeJS获取命令行后面的参数
- flask mysql环境配置_Flask教程4:数据库
- linux 分区顺序 boot,关于Liunx下的硬盘分区问题(/boot分区)?
- 第二节 数值、字符与字符串
- 程序员面试头条被拒,一年后逆袭拿到offer【巴分巴秒】
- DB2 数据库软件下载
- qq四国军旗2.1 beat03 builde018记牌器开发思路(四)
- 托业阅读时间怎么安排【zhasite】
- 10个国外免费下载电子书网站