C++ boost 簡單應用
簡介
C++的著名擴充函式庫
功能(節錄):
- regex
- function binding
- lambda functions
- unit tests
- smart pointers
- noncopyable, optional
- serialization
- generic dates
- portable filesystem
- circular buffers
- config utils
- generic image library
- program options
- threads
所有功能
安裝:sudo apt-get install libboost-all-dev
基本功能
簡化語法
foreach
1 |
|
實作方法
lexical_cast
可以轉換成其他格式
1 |
|
split
1 |
|
Smart Pointers
Basic properties of smart pointers
- no ownership at all
- smart pointer cannot delete the object, because it doesn't own it
- transfer of ownership
- only one smart pointer can ever point to the same object at the same time
- If the smart pointer is to be returned from functions, the ownership is transferred to the returned smart pointer
- Transfer of ownership cannot really be implemented in C++ currently, because object will be copied after return
- only can use the copy constructor to implement that transfer of ownership
- share of ownership
- multiple smart pointers can point to the same object at the same time
- can be implemented by having a copy constructor
Categorizing smart pointers
scoped_ptr
- neither transferable nor sharable (= normal pointer)
- when it goes out of scope, it is destroyed
shared_ptr
- shares ownership
- reference counted so it can see when the last copy of it goes out of scope and then it frees the object automatically
- 所有使用同一筆資料的指標都不再使用後自動釋放
weak_ptr
- non-owning smart pointer
- reference a managed object (managed by a shared_ptr) so it will not add a reference count
- If you need to access the object, you can lock the management of it (to avoid that in another thread a shared_ptr frees it while you use the object)
- If
weak_ptr
points to an object already deleted, it will notice you by throwing an exception - It is most beneficial when you have a cyclic reference: Reference counting cannot easily cope with such a situation
intrusive_ptr
- like a shared_ptr
- does not keep the reference count in a shared_ptr
- but call helper functions to increse/decrese the count
- helper function need to be defined by the object that is managed
- reference count is not anymore internal to the smart pointer, but the smart pointer uses an existing reference counting mechanism
- typically used when there is a 3rd party smart ptr you must use
- like a shared_ptr
unique_ptr
- transfer of ownership pointer
- cannot copy it, but can move it by using C++1x's
move
constructors
1 | std::vector< scoped_ptr<T> > tPtrVec; |
Chinese Explaination
1 |
|
Reference
Advantage of using boost libraries
Most used parts of boost