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 6753641
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T13:11:12+00:00 2026-05-26T13:11:12+00:00

For simplicity, let’s say I want to do implement a function which takes two

  • 0

For simplicity, let’s say I want to do implement a function which takes two parameters and predicate which test for equality,

template<typename T, typename TCompare>
bool Eq(const T& a, const T& b, const TCompare& cmp) {
  return cmp(a, b);
}

but I also want that operator== is assumed if predicate is not passed.

I tried several different approaches, but all resulted in either syntax error or “operator==” not defined. I even tried replacing the Eq function with a

Nevertheless, I suppose what I want is possible since the big part of STL supports not passing the optional TCompare parameter (std::sort, std::set …).

UPDATE:
Thanks to levis501 suggestion, this is the best way I have found so far:

template<typename T, typename TCompare>
bool Eq(const T& a, const T& b, const TCompare& cmp) {
  return cmp(a, b); // or some lengthy code
}

template<typename T>
bool Eq(const T& a, const T& b) {
  static std::equal_to<T> EqCmp;
  return Eq(a, b, EqCmp);
}

So, you basically need to create a wrapper which passes std::equal_to, or any other operator-equivalent functor. For some reason, having it as a default argument does not compile when you invoke the function without it.

  • 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-05-26T13:11:12+00:00Added an answer on May 26, 2026 at 1:11 pm

    How about std::equal_to ? http://www.cplusplus.com/reference/std/functional/equal_to/

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

For simplicity let's say I have two classes: a class called Car and a
For simplicity, let's say that I have two sets of words, sorted into alphabetical
I am creating a custom WPF control that let's say for simplicity sake has
For simplicity lets say I have two flex mxml pages. form.mxml button.mxml If the
For the sake of simplicity, let's say that we have input strings with this
I've two entities with 1 to N relation in between. Let's say Books and
For simplicity let's say I have code similar to this: def testMethod(String txt) {
Let's say I have two tables one called Customers and one called CustomerAddresses. Then
For sake of simplicity, let's assume I want to write an extension method for
Assume you are given a bunch of k-tensors, for simplicity let's say they are

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.