Spec-Zone.ru › C++

std::indirectly_unary_invocable, std::indirectly_regular_unary_invocable

Определено в заголовочном файле <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>>>;
(начиная с 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>>>;
(начиная с 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++.

DR Применяется к Поведение согласно опубликованной версии Правильное поведение
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>
не требует

© cppreference.com
Licensed under the Creative Commons Attribution-ShareAlike Unported License v3.0.
https://en.cppreference.com/cpp/iterator/indirectly_unary_invocable

Spec-Zone.ru

Настройки Оффлайн Что нового Помощь О нас
Spec-Zone .ru
спецификации, руководства, описания, API