C++14标准最近刚被经过,像之前同样,没有给这个语言带来太大变化,C++14标准是想经过改进C++11 来让程序员更加轻松的编程,C++11引入auto关键字(严格来讲auto从C++ 03 开始就有了,只是C++11改变了auto的含义),auto让你的代码更加干净,更容易让你避免错误,举个例子ios
原来你必须这样写c++
1
2
|
int
i =
1
;
std::complex<double> c = return_a_complex_number();
|
你如今能够这样写程序员
1
2
|
auto i =
1
;
auto c = return_a_complex_number();
|
声明为auto的变量在编译时期就分配了内存,而不是到了运行时期,因此使用auto再也不引起任何速度延迟,这也意味着使用auto的时候,这个变量不初始化会报错。由于编译器没法知道这个变量的类型。编程
1
|
auto i;
// this will rise a compilation error
|
C++11中一个作的很差的地方是,你不能使用auto来定义一个函数类型,在新的标准中你就能够了:函数
1
2
3
4
5
|
// Error in C++11, works in C++14
auto my_function() {
...
return
value;
}
|
只要你的函数返回一个值,编译器就知道怎么解释这个auto关键词。如今,你能够使用最新版本的Clang和GCC,性能
1
|
g++-
4.9
.
1
-Wall -std=c++
14
-pedantic my_prog_name.cpp -o my_prog_name
|
为了更好地使用auto来简化你的代码,让咱们分别使用C++98 、C++11 和C++14 来实现同一段代码,为了说明,咱们使用两个函数来改变一个vector变量this
1
|
multiply_by_two(add_one(my_vector));
|
很明显,这个循环给一个vector变量的每个值加一再乘以二 你能够写一个函数,而不是两个。这里咱们不是为了追求性能,而是说明auto的用法。spa
在C++98中你要这样写code
std::vector<int>& add_one(std::vector<int> &v) { for(std::vector<int>::iterator it = v.begin(); it != v.end(); it++) { *it += 1; } return v; } void multiply_by_two(std::vector<int> &v) { for(std::vector<int>::iterator it = v.begin(); it != v.end(); it++) { *it *= 2; } }
上面的代码显得很啰嗦ip
在C++11中你能够这样写
1
2
3
4
5
6
7
8
9
10
11
12
|
std::vector<
int
>& add_one(std::vector<
int
> &v) {
for
(auto& it : v) {
it +=
1
;
}
return
v;
}
void
multiply_by_two(std::vector<
int
> &v) {
for
(auto& it : v) {
it *=
2
;
}
}
|
C++11 中显然有了进步,咱们这是能够使用auto来简化循环时候的一点代码。但仍显啰嗦。
在C++14中你能够使用auto来定义一个函数类型,代码能够简化为这样:
1
2
3
4
5
6
7
8
9
10
11
12
|
auto& add_one(std::vector<
int
>& v) {
for
(auto& it : v) {
it +=
1
;
}
return
v;
}
void
multiply_by_two(std::vector<
int
>& v) {
for
(auto& it : v) {
it *=
2
;
}
}
|
这里是完整代码
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
|
// C++14 "auto" demo code
#
include
<iostream>
#
include
<vector>
auto& add_one(std::vector<
int
>& v) {
for
(auto& it : v) {
it +=
1
;
}
return
v;
}
void
multiply_by_two(std::vector<
int
>& v) {
for
(auto& it : v) {
it *=
2
;
}
}
void
print_vec(
const
std::vector<
int
>& v) {
for
(
const
auto& e: v) {
std::cout << e << std::endl;
}
std::cout << std::endl;
}
int
main() {
// Add one and multiply by two a vector of integers
std::vector<
int
> my_vector{-
1
,
2
,
3
,
5
};
multiply_by_two(add_one(my_vector));
print_vec(my_vector);
return
0
;
}
|
你能够清晰的对比出,C++14比C++11 有了一点进步。