Namespaces
Variants
Views
Actions

Difference between revisions of "cpp/iterator/indirectly unary invocable"

From cppreference.com
< cpp‎ | iterator
m (removes see also)
(~ apply P2997R1 as DR20)
 
(9 intermediate revisions by 6 users not shown)
Line 1: Line 1:
{{cpp/title|indirect_unary_invocable}}
+
{{cpp/title|indirectly_unary_invocable|indirectly_regular_unary_invocable}}
 
{{cpp/iterator/navbar}}
 
{{cpp/iterator/navbar}}
 
{{dcl begin}}
 
{{dcl begin}}
{{dcl header | iterator}}
+
{{dcl header|iterator}}
{{dcl | since=c++20 | 1=
+
{{dcl h|{{tt|std::indirectly_unary_invocable}}}}
 +
{{dcl|since=c++20|1=
 
template< class F, class I >
 
template< class F, class I >
concept indirectly_unary_invocable =
+
    concept indirectly_unary_invocable =
  std::indirectly_readable<I> &&
+
        std::indirectly_readable<I> &&
  std::copy_constructible<F> &&
+
        std::copy_constructible<F> &&
  std::invocable<F&, iter_value_t<I>&> &&
+
        std::invocable<F&, /*indirect-value-t*/<I>> &&
  std::invocable<F&, iter_reference_t<I>> &&
+
        std::invocable<F&, std::iter_reference_t<I>> &&
  std::invocable<F&, iter_common_reference_t<I>> &&
+
        std::common_reference_with<
  std::common_reference_with<
+
            std::invoke_result_t<F&, /*indirect-value-t*/<I>>,
    std::invoke_result_t<F&, std::iter_value_t<I>&>,
+
            std::invoke_result_t<F&, std::iter_reference_t<I>>>;
    std::invoke_result_t<F&, std::iter_reference_t<I>>>;
+
 
}}
 
}}
{{dcl | since=c++20 | 1=
+
{{dcl h|{{tt|std::indirectly_regular_unary_invocable}}}}
 +
{{dcl|since=c++20|1=
 
template< class F, class I >
 
template< class F, class I >
  concept indirectly_regular_unary_invocable =
+
    concept indirectly_regular_unary_invocable =
    std::indirectly_readable<I> &&
+
        std::indirectly_readable<I> &&
    std::copy_constructible<F> &&
+
        std::copy_constructible<F> &&
    std::regular_invocable<F&, iter_value_t<I>&> &&
+
        std::regular_invocable<F&, /*indirect-value-t*/<I>> &&
    std::regular_invocable<F&, iter_reference_t<I>> &&
+
        std::regular_invocable<F&, std::iter_reference_t<I>> &&
    std::regular_invocable<F&, iter_common_reference_t<I>> &&
+
        std::common_reference_with<
    std::common_reference_with<
+
            std::invoke_result_t<F&, /*indirect-value-t*/<I>>,
      std::invoke_result_t<F&, std::iter_value_t<I>&>,
+
            std::invoke_result_t<F&, std::iter_reference_t<I>>>;
      std::invoke_result_t<F&, std::iter_reference_t<I>>>;
+
 
}}
 
}}
 
{{dcl end}}
 
{{dcl end}}
  
The concepts {{tt|indirectly_unary_invocable}} and {{tt|indirectly_regular_unary_invocable}} specify requirements for algorithms that call (regular) unary invocables as their arguments. The key difference between these concepts and {{tt|std::invocable}} is that they are applied to the type the {{tt|I}} references, rather than {{tt|I}} itself.
+
The concepts {{tt|indirectly_unary_invocable}} and {{tt|indirectly_regular_unary_invocable}} specify requirements for algorithms that call (regular) unary invocables as their arguments. The key difference between these concepts and {{lc|std::invocable}} is that they are applied to the type the {{tt|I}} references, rather than {{tt|I}} itself.
  
===Semantic requirements===
+
===Notes===
{{tt|F}} and {{tt|I}} model their respective concepts only if all concepts they subsume are modeled.
+
The distinction between {{tt|indirectly_unary_invocable}} and {{tt|indirectly_regular_unary_invocable}} is purely semantic.
  
{{cpp/concepts/equality preservation}}
+
===Example===
 +
{{example|code=
 +
#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);
 +
}
 +
 
 +
|output=
 +
1 2 3 4 5 6 7 8 9
 +
}}
 +
 
 +
===Defect reports===
 +
{{dr list begin}}
 +
{{dr list item|paper=P2609R3|std=C++20|before=some requirements were defined in terms of {{c|std::iter_value_t<I>&}}<br>which mishandled projections resulting in incompatibility with invocable {{c|F&}}|after=defined in terms of {{c|/*indirect-value-t*/<I>}}<br>to correctly handle such projections}}
 +
{{dr list item|paper=P2997R1|std=C++20|before=corresponding concepts required {{c|F&}} to satisfy {{lconcept|invocable}} and<br>{{lconcept|regular_invocable}}, respectively, with {{c|std::iter_common_reference_t<I>}}|after=does not require}}
 +
{{dr list end}}
  
 
{{langlinks|es|ja|zh}}
 
{{langlinks|es|ja|zh}}

Latest revision as of 17:54, 23 August 2024

 
 
Iterator library
Iterator concepts
Iterator primitives
Algorithm concepts and utilities
Indirect callable concepts
indirectly_unary_invocableindirectly_regular_unary_invocable
(C++20)(C++20)  
Common algorithm requirements
(C++20)
(C++20)
(C++20)
Utilities
(C++20)
Iterator adaptors
Range access
(C++11)(C++14)
(C++14)(C++14)  
(C++11)(C++14)
(C++14)(C++14)  
(C++17)(C++20)
(C++17)
(C++17)
 
Defined in header <iterator>
std::indirectly_unary_invocable
template< class F, class I >

    concept indirectly_unary_invocable =
        std::indirectly_readable<I> &&
        std::copy_constructible<F> &&
        std::invocable<F&, /*indirect-value-t*/<I>> &&
        std::invocable<F&, std::iter_reference_t<I>> &&
        std::common_reference_with<
            std::invoke_result_t<F&, /*indirect-value-t*/<I>>,

            std::invoke_result_t<F&, std::iter_reference_t<I>>>;
(since C++20)
std::indirectly_regular_unary_invocable
template< class F, class I >

    concept indirectly_regular_unary_invocable =
        std::indirectly_readable<I> &&
        std::copy_constructible<F> &&
        std::regular_invocable<F&, /*indirect-value-t*/<I>> &&
        std::regular_invocable<F&, std::iter_reference_t<I>> &&
        std::common_reference_with<
            std::invoke_result_t<F&, /*indirect-value-t*/<I>>,

            std::invoke_result_t<F&, std::iter_reference_t<I>>>;
(since C++20)

The concepts indirectly_unary_invocable and indirectly_regular_unary_invocable specify requirements for algorithms that call (regular) unary invocables as their arguments. The key difference between these concepts and std::invocable is that they are applied to the type the I references, rather than I itself.

[edit] Notes

The distinction between indirectly_unary_invocable and indirectly_regular_unary_invocable is purely semantic.

[edit] Example

#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);
}

Output:

1 2 3 4 5 6 7 8 9

[edit] Defect reports

The following behavior-changing defect reports were applied retroactively to previously published C++ standards.

DR Applied to Behavior as published Correct behavior
P2609R3 C++20 some requirements were defined in terms of std::iter_value_t<I>&
which mishandled projections resulting in incompatibility with invocable F&
defined in terms of /*indirect-value-t*/<I>
to correctly handle such projections
P2997R1 C++20 corresponding concepts required F& to satisfy invocable and
regular_invocable, respectively, with std::iter_common_reference_t<I>
does not require