根據cppreference建構式(2)的std::span定義為
template< class It >
explicit(extent != std::dynamic_extent)
constexpr span( It first, size_type count );
其例外情況列為2) Throws nothing.
如果這個建構式“什么都不拋出”,那么為什么它甚至被列在例外下,為什么建構式沒有標記為 noexcept?
uj5u.com熱心網友回復:
那是因為這個建構式有先決條件。從標準:
template<class It>
constexpr explicit(extent != dynamic_extent) span(It first, size_type count);約束:設 U 為 remove_reference_t<iter_reference_t>。
- 它滿足 contiguous_iterator。
- is_convertible_v<U( )[], element_type( )[]> 為真。
[注 1:目的是只允許迭代器參考型別到 element_type 的限定轉換。——尾注]前提條件:
- [first, first count) 是一個有效范圍。
- 它對 contiguous_iterator 進行建模。
- 如果extent 不等于dynamic_extent,那么count 等于extent。
效果:用 to_address(first) 初始化 data_,用 count 初始化 size_。
拋出:什么都沒有。
具有“不拋出任何東西”的先決條件的函式不會被標記noexcept,因為如果不滿足這些先決條件,可能會發生未定義的行為。例如,即使它不會拋出,也std::vector::front不會被標記,但是在空向量上呼叫它是未定義的行為。noexcept
這是一篇關于它的論文:https ://www.open-std.org/jtc1/sc22/wg21/docs/papers/2020/p1656r1.html
轉載請註明出處,本文鏈接:https://www.uj5u.com/qiye/529624.html
標籤:C 例外stl无例外
上一篇:ControllerAdvice在SpringSecurity之前捕獲AuthenticationException
