Did you already learn what are C++ concepts?

In C++20 we can do this:

#include <ranges>
#include <vector>

template <std::ranges::range Range>
    requires std::same_as<std::ranges::range_value_t<Range>, int>
auto TransformIt(Range r)
{
    return r | std::views::transform([](int n) { return n * n; });
}

int main()
{
    std::vector<int> v;
    auto r = TransformIt(v);
    return 0;
}

or improve our code with this:

template <class R, class Value>
concept range_over = std::ranges::range<R> && 
    std::same_as<std::ranges::range_value_t<R>, Value>;

template <range_over<int> Range>
auto TransformIt(Range r)
{
    return r | std::views::transform([](int n) { return n * n; });
}

Or you still work for some company in Russia where the prohibition of use dynamic_cast is the most important architectural solution?

Leave a Reply

Your email address will not be published. Required fields are marked *