1

I'm trying to wrap my head around how ranges work. The following code seems natural to me:

#include <algorithm>
#include <map>
#include <ranges>

int main()
{
    std::multimap<int, int> a{
        { 1, 2 },
        { 1, 3 },
        { 4, 5 },
        { 4, 6 },
    };
    const auto [b, c] = a.equal_range(a.cbegin()->first);
    auto d = std::ranges::min(std::ranges::subrange{ b, c }, {}, &decltype(a)::value_type::second);
}

But it won't compile with this error (shortened):

main.cpp(14,24): error C2672: 'operator __surrogate_func': no matching overloaded function found
main.cpp(14,95): error C7602: 'std::ranges::_Min_fn::operator ()': the associated constraints are not satisfied
algorithm(9663): message : see declaration of 'std::ranges::_Min_fn::operator ()'

So my question is, why not? And what should I do differently to make this work?

So to be clear, of all the elements in the multimap associated with the first key, I want the element with the lowest value.

I'm compiling with Visual Studio 2019 16.11.1 with the /std:c++latest flag.

Godbold example

Marek R
  • 32,568
  • 6
  • 55
  • 140
Jupiter
  • 1,421
  • 2
  • 12
  • 31
  • what you was expecting that min call to do? you feed it a range, an empty initializer list and a callable. – Swift - Friday Pie Oct 19 '21 at 10:08
  • I want the min call to loop through the elements the subrange points to (which should be all elements in the multimap that share the first key), and then return me the element (an std::pair) that has the smallest value (value meaning pair.second). – Jupiter Oct 19 '21 at 10:12
  • Can be reduced to `std::vector>` [Demo](https://godbolt.org/z/haco39fbj). (for `/*multi*//*unordered*/map`, key is `const`) – Jarod42 Oct 19 '21 at 10:23
  • 1
    constraint for min not met , because not copyable\movable, afaik. – Swift - Friday Pie Oct 19 '21 at 10:34
  • Fixed version: https://godbolt.org/z/EajdG8Wjc – Marek R Oct 19 '21 at 14:28
  • 1
    @MarekR In the end, it turned out I actually wanted the value of the resulting element so I ended up using https://godbolt.org/z/MEe787K88 – Jupiter Oct 19 '21 at 16:19

0 Answers0