問題描述
noexcept
關鍵字可以適當地應用于許多函數簽名,但我不確定何時應該考慮在實踐中使用它.根據我目前所讀到的內容,最后一分鐘添加的 noexcept
似乎解決了移動構造函數拋出時出現的一些重要問題.但是,對于一些實際問題,我仍然無法提供令人滿意的答案,這些問題使我首先閱讀了有關 noexcept
的更多信息.
The noexcept
keyword can be appropriately applied to many function signatures, but I am unsure as to when I should consider using it in practice. Based on what I have read so far, the last-minute addition of noexcept
seems to address some important issues that arise when move constructors throw. However, I am still unable to provide satisfactory answers to some practical questions that led me to read more about noexcept
in the first place.
有很多我知道永遠不會拋出的函數示例,但編譯器無法自行確定.在所有這些情況下,我應該將
noexcept
附加到函數聲明中嗎?
There are many examples of functions that I know will never throw, but for which the compiler cannot determine so on its own. Should I append
noexcept
to the function declaration in all such cases?
不得不考慮是否需要在 every 函數聲明之后附加 noexcept
會大大降低程序員的工作效率(坦率地說,會很痛苦).在哪些情況下我應該更小心地使用 noexcept
,哪些情況下我可以使用隱含的 noexcept(false)
?
Having to think about whether or not I need to append noexcept
after every function declaration would greatly reduce programmer productivity (and frankly, would be a pain in the neck). For which situations should I be more careful about the use of noexcept
, and for which situations can I get away with the implied noexcept(false)
?
在使用 noexcept
后,我什么時候才能真正看到性能改進?特別地,給出一個代碼示例,在添加 noexcept
后,C++ 編譯器能夠為其生成更好的機器代碼.
When can I realistically expect to observe a performance improvement after using noexcept
? In particular, give an example of code for which a C++ compiler is able to generate better machine code after the addition of noexcept
.
就我個人而言,我關心 noexcept
因為為編譯器提供了更多的自由來安全地應用某些類型的優化.現代編譯器會以這種方式利用 noexcept
嗎?如果沒有,我可以期待他們中的一些人在不久的將來這樣做嗎?
Personally, I care about noexcept
because of the increased freedom provided to the compiler to safely apply certain kinds of optimizations. Do modern compilers take advantage of noexcept
in this way? If not, can I expect some of them to do so in the near future?
推薦答案
我認為現在給出最佳實踐"答案還為時過早,因為沒有足夠的時間在實踐中使用它.如果在拋出說明符出現后立即問到這個問題,那么答案將與現在大不相同.
I think it is too early to give a "best practices" answer for this as there hasn't been enough time to use it in practice. If this was asked about throw specifiers right after they came out then the answers would be very different to now.
必須考慮是否需要在每個函數聲明后附加 noexcept
會大大降低程序員的工作效率(坦率地說,這會很痛苦).
Having to think about whether or not I need to append
noexcept
after every function declaration would greatly reduce programmer productivity (and frankly, would be a pain).
好吧,那么在很明顯函數永遠不會拋出時使用它.
Well, then use it when it's obvious that the function will never throw.
在使用 noexcept
后,我什么時候才能真正看到性能改進?[...] 就我個人而言,我關心 noexcept
因為增加了編譯器的自由度,可以安全地應用某些類型的優化.
When can I realistically expect to observe a performance improvement after using
noexcept
? [...] Personally, I care aboutnoexcept
because of the increased freedom provided to the compiler to safely apply certain kinds of optimizations.
似乎最大的優化收益來自用戶優化,而不是編譯器優化,因為可能會檢查 noexcept
并對其進行重載.大多數編譯器遵循無懲罰如果你不拋出異常處理方法,所以我懷疑它會在你的代碼的機器代碼級別上改變很多(或任何東西),盡管可能通過刪除處理代碼.
It seems like the biggest optimization gains are from user optimizations, not compiler ones due to the possibility of checking noexcept
and overloading on it. Most compilers follow a no-penalty-if-you-don't-throw exception handling method, so I doubt it would change much (or anything) on the machine code level of your code, although perhaps reduce the binary size by removing the handling code.
在四大(構造函數、賦值函數,而不是析構函數,因為它們已經是 noexcept
)中使用 noexcept
可能會像 noexcept
那樣帶來最好的改進code> 檢查在模板代碼中是常見的",例如在 std
容器中.例如,std::vector
不會使用你的類的移動,除非它被標記為 noexcept
(否則編譯器可以推斷出它).
Using noexcept
in the big four (constructors, assignment, not destructors as they're already noexcept
) will likely cause the best improvements as noexcept
checks are 'common' in template code such as in std
containers. For instance, std::vector
won't use your class's move unless it's marked noexcept
(or the compiler can deduce it otherwise).
這篇關于我什么時候應該真正使用 noexcept?的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網!