What is new in C++11 (Some of concept is from previous version, so take this as a general C study note)
https://www.devbean.net/2012/11/biggest-changes-in-c11/
https://read01.com/mAkeBg.html#.Ws016shlBbX
2. Lamda expression
3. Auto
auto x = 11; // x is int
auto y = 'a'; // y is char
(The difference between Auto and the Initial of Python? They are similar, auto can be any type depending on the operand following, the initialization of python acts the same. However, Auto in C++ can not change type after it declaration)
ps. Auto is also not really new in C++11, the ANCI C has this keyword in different meaning
4. Integrate Initial syntax
5. Default/ Delete
struct A
{
A() = default; // C++11
virtual ~A() = default; // C++11
};
6. Rvalue reference
https://zh.wikipedia.org/wiki/%E5%8F%B3%E5%80%BC%E5%BC%95%E7%94%A8
this gives clear example
https://liam0205.me/2016/12/11/rvalue-reference-in-Cpp/
https://charlottehong.blogspot.com/2017/03/stdmove.html
https://www.ibm.com/developerworks/cn/aix/library/1307_lisl_c11/
C++11 support move semantics ( compare with copy semantics, which need temporary space). Every use of temporary space means allocate a space, copy value and destroy, which really exhausted.
This is from ptt.cc, using a very simple explanation:
https://www.ptt.cc/bbs/C_and_CPP/M.1469456171.A.23F.html
https://www.ptt.cc/bbs/C_and_CPP/M.1469465868.A.A4C.html
Traditionally: when you wants to swap (int a) and (int b), you have to use a (int tmp):
when you write:

You actually allocate a memory space for (int tmp) and write value into this address.
Free this memory space when program terminate. This is exhausted.
C++11 support move()
When you use the move(), for example, a = move(b).
You actually saying: I am not going to use 'b' anymore, this address is called 'a' (holds b's value). Such thing is faster than traditional copy-assign.
7. STD:
(1). Smart Pointer
https://kheresy.wordpress.com/2012/03/03/c11_smartpointer_p1/
https://kheresy.wordpress.com/2012/03/05/c11_smartpointer_p2/
Usually we have to delete everything we new (free everything we allocate). Otherwise it cause memory leak.
C++11 provide three kinds of pointer: (a) unique_ptr (b) shared_ptr (c) weak_ptr
( #incluse <memory> before we use)
(a) unique_ptr
A memory space can only used by a single object (which assigned by a particular pointer). When this function end, this memory space will automatically free. (No need to delete it manually)
(b) shared_ptr
Allow multiple objective share a memory space. This memory will automatically free until all function with reference object end.
( reference count is check-able in STL)
(c) weak_ptr
Not used for data access. Monitor the use condition of shared_ptr
ps. Before c++11, we can also use auto_ptr to achieve this, auto_ptr = unique_ptr(2) Thread library
http://www.devx.com/SpecialReports/Article/38883
(3) Other new API
#include <algorithm>
// All element positive?
all_of(first, first+n, ispositive()); // false
// At least one element positive?
any_of(first, first+n, ispositive()); // true
// None of element is positive?
none_of(first, first+n, ispositive()); // false
int source[5] = { 0, 12, 34, 50, 80 };
int target[5];
// cpoy 5 element from source to target
copy_n(source, 5, target);
X. RAII
沒有留言:
張貼留言