std::indirectly_unary_invocable, std::indirectly_regular_unary_invocable
定義於標頭檔案 <iterator> |
||
std::indirectly_unary_invocable |
||
template< class F, class I > concept indirectly_unary_invocable = |
(C++20 起) | |
std::indirectly_regular_unary_invocable |
||
template< class F, class I > concept indirectly_regular_unary_invocable = |
(C++20 起) | |
概念 `indirectly_unary_invocable` 和 `indirectly_regular_unary_invocable` 為演算法指定了對其引數呼叫(正則)一元可呼叫的要求。這些概念與 std::invocable 之間的主要區別在於,它們應用於 `I` 引用的型別,而不是 `I` 本身。
[編輯] 注意
`indirectly_unary_invocable` 和 `indirectly_regular_unary_invocable` 之間的區別純粹是語義上的。
[編輯] 示例
#include <algorithm> #include <iterator> #include <print> #include <ranges> struct IntWrapper { int i; explicit IntWrapper(int i) : i(i) {} IntWrapper(IntWrapper&&) = default; IntWrapper& operator=(IntWrapper&&) = default; }; int main() { auto ints = std::views::iota(1, 10); auto print = [] (IntWrapper w) { std::print("{} ", w.i); }; auto wrap = [] (int i) { return IntWrapper{i}; }; using Proj = std::projected<decltype(ints.begin()), decltype(wrap)>; // error (evaluated to false) until P2609R3: // this was because 'std::iter_value_t<Proj> &' is the same as 'IntWrapper&' // which is not convertible to 'IntWrapper' (implicitly deleted copy ctor) static_assert(std::indirectly_unary_invocable<decltype(print), Proj>); // if the compile-time check above evaluates to true, then this is well-formed: std::ranges::for_each(ints, print, wrap); }
輸出
1 2 3 4 5 6 7 8 9
[編輯] 缺陷報告
下列更改行為的缺陷報告追溯地應用於以前出版的 C++ 標準。
缺陷報告 | 應用於 | 釋出時的行為 | 正確的行為 |
---|---|---|---|
P2609R3 | C++20 | 一些要求是根據 std::iter_value_t<I>& 定義的 這錯誤地處理了投影,導致與可呼叫 F& 不相容 |
根據 /*indirect-value-t*/<I> 定義 以正確處理此類投影 |
P2997R1 | C++20 | 相應的概念要求 F& 分別滿足 `invocable` 和 `regular_invocable`,以及 std::iter_common_reference_t<I> |
不再需要 |