編譯器: MSVC v142
所屬文件: xutility
位置(行): 377
函數(shù)名稱:_Get_unwrapped_n
函數(shù)描述:
嘗試提取迭代器的_Ptr
, 如果迭代器沒有_ptr
則原封不動的返回這個迭代器
源碼:
template <class _Iter, class _Diff,
enable_if_t<
!_Unwrappable_for_unverified_v<_Iter> //
&& ((!_Unwrappable_for_offset_v<_Iter> && is_integral_v<_Diff>) || is_same_v<_Diff, _Distance_unknown>),
int> = 0>
_NODISCARD constexpr const _Iter& _Get_unwrapped_n(const _Iter& _It, _Diff) {
// pass through lvalue iterator that doesn't participate in checking
return _It;
}
?
拆分理解
-
_Unwrappable_for_unverified_v
template <class _Iter> _INLINE_VAR constexpr bool _Unwrappable_for_unverified_v = _Unwrappable_v<_Iter>&& _Do_unwrap_when_unverified_v<_Iter>;
_Unwrappable_v 驗證模板參數(shù)是不是一個
Iterator
.
_Do_unwrap_when_unverified_v 判斷是否可以執(zhí)行_Unwrapped
函數(shù).vector<int> a {1, 2, 3, 4, 5}; vector<int> b {}; auto start_iter = a.begin(); // 生成一個 iterator: _Vector_iterator auto back_iter = back_inserter(b); // 生成一個 iterator: back_insert_iterator // Debug 模式 cout << _ITERATOR_DEBUG_LEVEL << endl; // 2 cout << _Unwrappable_for_unverified_v<decltype(start_iter)> << endl; // false cout << _Unwrappable_for_unverified_v<decltype(back_iter)> << endl; // false // Release 模式 cout << _ITERATOR_DEBUG_LEVEL << endl; // 0 cout << _Unwrappable_for_unverified_v<decltype(start_iter)> << endl; // true cout << _Unwrappable_for_unverified_v<decltype(back_iter)> << endl; // false
這個測試代碼說明,當(dāng)編譯模式是
Debug
時,begin
和back_inserter
生成的iterator
都是不允許在未驗證_Verify_range
的情況下調(diào)用iterator
的_Unwrapped
函數(shù). 當(dāng)編譯模式是Release
時,begin
生成的iterator
允許在未驗證_Verify_range
的情況下調(diào)用iterator
的_Unwrapped
函數(shù), 而back_inserter
則不可以.
備注
: 這里說的不允許, 并不是真的不能調(diào)用, 而是做模板判斷的時候, 它會過濾掉當(dāng)前篩選. -
_Unwrappable_for_offset_v
template <class _Iter> _INLINE_VAR constexpr bool _Unwrappable_for_offset_v = _Unwrappable_v<_Iter>&& _Offset_verifiable_v<_Iter>;
_Unwrappable_v 驗證模板參數(shù)是不是一個
Iterator
.
_Offset_verifiable_v 判斷是否可以執(zhí)行_Verify_offset
函數(shù). -
is_integral_v
// determine whether cv-unqualified type _Ty is integral template <class _Ty> _INLINE_VAR constexpr bool _Is_integral = _Is_any_of_v<_Ty, bool, char, signed char, unsigned char, wchar_t, char16_t, char32_t, short, unsigned short, int, unsigned int, long, unsigned long, long long, unsigned long long>; template <class _Ty> _INLINE_VAR constexpr bool is_integral_v = _Is_integral<remove_cv_t<_Ty>>;
判斷一個
type
是不是一個數(shù)字類型. -
is_same_v
template <class, class> _INLINE_VAR constexpr bool is_same_v = false; // determine whether arguments are the same type template <class _Ty> _INLINE_VAR constexpr bool is_same_v<_Ty, _Ty> = true;
如果第一個類型參數(shù)和第二個類型參數(shù)相同, 則會匹配到
template <class _Ty> _INLINE_VAR constexpr bool is_same_v<_Ty, _Ty> = true;
這個特化版本, 表示true
.
如果第一個類型參數(shù)和第二個類型參數(shù)不同, 則會跳過第一個匹配, 命中template <class, class> _INLINE_VAR constexpr bool is_same_v = false;
這個標(biāo)準(zhǔn)模板, 表示false
.