c++11提供了關鍵字noexcept,用來指明某個函式無法——或不打算——拋出例外:
void foo() noexcept; // a function specified as will never throw
void foo2() noexcept(true); // same as foo
void bar(); // a function might throw exception
void bar2() noexcept(false); // same as bar
所以我們需要了解以下兩點:
noexcept有什么優點,例如性能、可讀性等等,- 需不需要在代碼中大量使用
noexcept,
noexcept優點
我們先從std::vector入手來看一下第一點,
我們知道,vector有自己的capacity,當我們呼叫push_back但是vector容量滿時,vector會申請一片更大的空間給新容器,將容器內原有的元素copy到新容器內:

但是如果在擴容元素時出現例外怎么辦?
- 申請新空間時出現例外:舊vector還是保持原有狀態,拋出的例外交由用戶自己處理,
- copy元素時出現例外:所有已經被copy的元素利用元素的解構式釋放,已經分配的空間釋放掉,拋出的例外交由用戶自己處理,
這種擴容方式比較完美,有例外時也會保持上游呼叫push_back時原有的狀態,
但是為什么說比較完美,因為這里擴容還是copy的,當vector內是一個類且持有資源較多時,這會很耗時,所以c++11推出了一個新特性:move,它會將資源從舊元素中“偷”給新元素(對move不熟悉的同學可以自己查下資料,這里不展開說了),應用到vector擴容的場景中:當vector中的元素的移動拷貝建構式是noexcept時,vector就不會使用copy方式,而是使用move方式將舊容器的元素放到新容器中:

利用move的交換類資源所有權的特性,使用vector擴容效率大大提高,但是當發生例外時怎么辦:
原有容器的狀態已經被破壞,有部分元素的資源已經被偷走,若要恢復會極大增加代碼的復雜性和不可預測性,所以只有當vector中元素的move constructor是noexcept時,vector擴容才會采取move方式來提高性能,
剛才總結了利用noexcept如何提高vector擴容,實際上,noexcept還大量應用在swap函式和move assignment中,原理都是一樣的,
noexcept使用場景
上面提到了noexcept可以使用的場景:
- move constructor
- move assignment
- swap
很多人的第一念頭可能是:我的函式現在看起來明顯不會拋例外,又說宣告noexcept編譯器可以生成更高效的代碼,那能加就加唄,但是事實是這樣嗎?
這個問題想要討論清楚,我們首先需要知道以下幾點:
- 函式自己不拋例外,但是不代表它們內部的呼叫不會拋出例外,并且編譯器不會提供呼叫者與被呼叫者的
noexcept一致性檢查,例如下述代碼是合法的:
void g(){
... //some code
}
void f() noexcept
{
… //some code
g();
}
- 當一個宣告為
noexcept的函式拋出例外時,程式會被終止并呼叫std::terminate();
所以在我們的代碼內部呼叫復雜,鏈路較長,且隨時有可能加入新feature時,過早給函式加上noexcept可能不是一個好的選擇,因為noexcept一旦加上,后續再去掉也會變得困難 : 呼叫方有可能看到你的函式宣告為noexcept,呼叫方也會宣告為noexcept,但是當你把函式的noexcept去掉卻沒有修改呼叫方的代碼時,當例外拋出到呼叫方會導致程式終止,
目前主流的觀點是:
- 加noexcept
- 函式在c++98版本中已經被宣告為
throw() - 上文提到過的三種情況:move constructor、move assignmemt、swap,如果這些實作不拋出例外,一定要使用
noexcept, - leaf function. 例如獲取類成員變數,類成員變數的簡單運算等,下面是stl的正向iterator中的幾個成員函式:
- 函式在c++98版本中已經被宣告為
# if __cplusplus >= 201103L
# define _GLIBCXX_NOEXCEPT noexcept
# else
# define _GLIBCXX_NOEXCEPT
reference
operator*() const _GLIBCXX_NOEXCEPT
{ return *_M_current; }
pointer
operator->() const _GLIBCXX_NOEXCEPT
{ return _M_current; }
__normal_iterator&
operator++() _GLIBCXX_NOEXCEPT
{
++_M_current;
return *this;
}
__normal_iterator
operator++(int) _GLIBCXX_NOEXCEPT
{ return __normal_iterator(_M_current++); }
- 不加noexcept
除了上面的要加的情況,其余的函式不要加noexcept就可以,
最后我們看一下vector如何實作利用noexcept move constructor擴容以及move constructor是否宣告noexcept對擴容的性能影響,
如何實作利用noexcept move constructor擴容
這里就不貼大段的代碼了,每個平臺的實作可能都不一樣,我們只關注vector是怎么判斷呼叫copy constructor還是move constructor的,
其中利用到的核心技術有:
- type trait
- iterator trait
- move iterator
- std::forward
核心代碼:
template <typename _Iterator, typename _ReturnType = typename conditional<
__move_if_noexcept_cond<typename iterator_traits<_Iterator>::value_type>::value,
_Iterator, move_iterator<_Iterator>>::type>
inline _GLIBCXX17_CONSTEXPR _ReturnType __make_move_if_noexcept_iterator(_Iterator __i) {
return _ReturnType(__i);
}
template <typename _Tp>
struct __move_if_noexcept_cond
: public __and_<__not_<is_nothrow_move_constructible<_Tp>>, is_copy_constructible<_Tp>>::type {};
這里用type trait和iterator trait聯合判斷:假如元素有noexcept move constructor,那么is_nothrow_move_constructible=1 => __move_if_noexcept_cond=0 => __make_move_if_noexcept_iterator回傳一個move iterator,這里move iterator迭代器配接器也是一個c++11新特性,用來將任何對底層元素的處理轉換為一個move操作,例如:
std::list<std::string> s;
std::vector<string> v(make_move_iterator(s.begin()),make_move_iterator(s.end())); //make_move_iterator回傳一個std::move_iterator
然后上游利用生成的move iterator進行回圈元素move:
{
for (; __first != __last; ++__first, (void)++__cur) std::_Construct(std::__addressof(*__cur), *__first);
return __cur;
}
template <typename _T1, typename... _Args>
inline void _Construct(_T1 *__p, _Args &&... __args) {
::new (static_cast<void *>(__p)) _T1(std::forward<_Args>(__args)...); //實際copy(或者move)元素
}
其中_Construct就是實際copy(或者move)元素的函式,這里很關鍵的一點是:對move iterator進行解參考操作,回傳的是一個右值參考,,這也就保證了,當__first型別是move iterator時,用_T1(std::forward<_Args>(__args)...進行“完美轉發”才呼叫_T1型別的move constructor,生成的新物件被放到新vector的__p地址中,
總結一下程序就是:
- 利用
type trait和iterator trait生成指向舊容器的normal iterator或者move iterator - 回圈將舊容器的元素搬到新容器,如果指向舊容器的是
move iterator,那么解參考會回傳右值參考,會呼叫元素的move constructor,否則呼叫copy constructor,
大家可以用下面這段簡單的代碼在自己的平臺打斷點除錯一下:
class A {
public:
A() { std::cout << "constructor" << std::endl; }
A(const A &a) { std::cout << "copy constructor" << std::endl; }
A(const A &&a) noexcept { std::cout << "move constructor" << std::endl; }
};
int main() {
std::vector<A> v;
for (int i = 0; i < 10; i++) {
A a;
v.push_back(a);
}
return 0;
}
noexcept move constructor對性能的影響
這篇文章C++ NOEXCEPT AND MOVE CONSTRUCTORS EFFECT ON PERFORMANCE IN STL CONTAINERS介紹了noexcept move constructor對耗時以及記憶體的影響,這里不重復贅述了,感興趣的可以自己試一下,

參考資料:
- When to Use noexcept And When to Not
- Does noexcept improve performance?
- EffectiveModernCppChinese Item14
- C++11的noexcept識別符號與運算子應如何正確使用?
(完)
朋友們可以關注下我的公眾號,獲得最及時的更新:

轉載請註明出處,本文鏈接:https://www.uj5u.com/houduan/228703.html
標籤:其他
