Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8193053
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 7, 20262026-06-07T04:20:47+00:00 2026-06-07T04:20:47+00:00

I am trying to implement a custom binary search to run over a vector

  • 0

I am trying to implement a custom binary search to run over a vector of dates.

My binary search function is the following:

template <typename RandomAccessIterator, typename Value, typename Comparer>
inline int binary_search(RandomAccessIterator const  first, RandomAccessIterator const  last, Value const& value, Comparer comparer)
{
    RandomAccessIterator it(std::lower_bound(first, last, value, comparer));
    if (it == last || comparer(*it, value) || comparer(value, *it))
      return distance(first,last);

    return distance(first,it);
}

The comparator I am using is defined as:

template <class T>
inline bool cmp(T lhs,T rhs)
{
  return lhs<rhs;
}

These two compile without issues, however, I get a compilation error when I try to call the binary_search function using the following code:

binary_search(date_list.begin(),date_list.end(),date2,cmp)

where date_list is a vector containing dates, date2 is an int.

The exact error message is:

error: no matching function for call to ?binary_search(__gnu_cxx::__normal_iterator<int*, std::vector<int, std::allocator<int> > >, __gnu_cxx::__normal_iterator<int*, std::vector<int, std::allocator<int> > >, int&, <unresolved overloaded function type>)?

Any ideas on how to solve this?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-07T04:20:50+00:00Added an answer on June 7, 2026 at 4:20 am

    You’re passing the name of a template (cmp) in a context where C++ wants a value. Apart from the fact that you can’t do this, it’s a chicken-or-egg problem: what type is cmp? A function’s type depends on its arguments, and this function can take arguments of any type. So what type does the compiler infer for the template argument Comparer? It would have to look at the body of the function to figure out that you expect int, and that’s not always possible—the compiler doesn’t always have access to the source code of templates.

    You need to specifically select the type for the function template that you’re passing. For example:

    binary_search(date_list.begin(), date_list.end(), date2, cmp<int>);
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm trying to implement a custom menu in my joomla template. I added this
I'm trying to implement a custom aggregate function in Postgres which will average directions
I was trying to implement custom validation attribute by inheriting from ValidationAttribute and found
im trying to implement a custom error page, what i want to be able
I'm trying to implement a custom field type for my Symfony2 forms but for
I'm trying to implement a custom route in rails match '/:slug' => products#get_by_slug, :as
I'm trying to implement some custom model metadata in ASP.NET MVC 3. I can't
I'm trying to implement a custom tab bar which is scrollable and has paging
I'm trying to implement a custom control in C# and I need to get
I'm trying to implement a custom form builder, similar to those provided by Wufoo

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.