2021-02-28

error: no match for call to '(const std::ranges::__sort_fn)

I was practicing vectors and ranges in c++ 20 was stuck at following state.

#include <iostream>
#include <vector>
#include <random>
#include <ranges>
#include <algorithm>

namespace ranges = std::ranges;

struct Model
{
    double next_event_time;
};

std::vector<Model> generate_examples(int number)
{
    // A uniform random number generator object
    // Used as the source of randomness
    std::default_random_engine generator;

    // Calls () operator on generator to get uniformly-distributed integers
    // then transforms the obtained values to output the disired distribution

    // Will uniformly generate values between 0 ~ 1
    std::uniform_real_distribution<double> distribution(0.0, 1.0);

    std::vector<Model> models;

    for (auto i = 0; i < number; i++)
    {
        models.push_back(Model{.next_event_time = distribution(generator)});
    }

    return models;
}

Model get_next_model(const std::vector<Model> &models)
{
    ranges::sort(models | std::views::transform([](const Model &x) { return x.next_event_time; }));

    return models[0];
}

int main()
{
    std::vector<Model> models = generate_examples(10);

    for (const auto &model : models)
        std::cout << model.next_event_time << std::endl;
}

I compiled the code with g++ 10.2 and got error

 error: no match for call to '(const std::ranges::__sort_fn) ~~~
ranges::sort(models | std::views::transform([](const Model &x) { return x.next_event_time; }));

Instead of std::views::transform, I also tried

  1. lambda expression
  2. ranges::sort(models, {}, &Model::next_event_time)

But they all produced similar no match for call to error. Why is this happening?



from Recent Questions - Stack Overflow https://ift.tt/3sCF2c3
https://ift.tt/eA8V8J

1 comment:

  1. wonderful article. Very interesting to read this article.I would like to thank you for the efforts you had made for writing this awesome article. This article resolved my all queries. keep it up.
    AWS Training Hyderabad/
    AWS Course Hyderabad/
    AWS Training Institutes Hyderabad/
    AWS Online Training Hyderabad/

    ReplyDelete