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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 16, 20262026-05-16T05:13:46+00:00 2026-05-16T05:13:46+00:00

I’m working on a problem in C++ that involves lots of subset and transformation

  • 0

I’m working on a problem in C++ that involves lots of subset and transformation operations on a large quantity of data. To that end, I’ve created a map function and something like list comprehensions. I’ve found that a bunch of the predicates I’m writing also have inverses, so I’d need to write:

template <typename type_t>
bool HasTenFoo(const type_t &t) {
  return t.foo >= 10.0;
}

and

template <typename type_t>
bool DoesntHaveTenFoo(const type_t &t) {
  return t.foo < 10.0;
}

Neither of those is a real example, but they’re representative. I’m also using a fair number of functors like:

class HasEnoughFoo {
public:
  HasEnoughFoo (double bar) { this->bar = bar; }
  template<typename type_t>
  bool operator()(const type_t &t) const { return t.foo >= bar; }
private:
  double bar;
};

some of which should have inverses as well. Rather than duplicate code unnecessarily, I’d like to write a functor that takes a predicate as an argument and returns the (value of the) inverse of that predicate. My fist cut at one is below:

/* -- Returns the opposite of some other predicate -------------------------- */

template<typename predicate_t>
class Not {
public:
  template <typename predicate_t>
  Not(predicate_t *p) { predicate = p; }

  template <typename type_t>
  bool operator()(const type_t &t) const {
    return !(*predicate)(t);
  }

private:
  predicate_t *predicate;
};

I would call that with something like:

new_list = old_list.subset(Not<HasEnoughFoo>(&HasEnoughFoo(10.0));

or

new_list = old_list.subset(Not<HasTenFoo>(&HasTenFoo));

That seems to work well when predicate_t is a functor like HasEnoughFoo, but fails when predicate_t refers to a regular function like HasTenFoo.

Visual Studio complains that 'HasTenFoo' is not a valid template type argument for parameter 'predicate_t'. Is there any way to write a Not() predicate that will work with functors and functions or am I doomed to write dozens of predicates and their inverses as well?

  • 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-16T05:13:47+00:00Added an answer on May 16, 2026 at 5:13 am

    Here’s an example of your code made to work (I removed the foo member, so it would work with just doubles).

    template <typename type_t>
    bool HasTenFoo(const type_t &t) {
      return t >= 10.0;
    }
    
    class HasEnoughFoo {
    public:
      HasEnoughFoo (double bar) { this->bar = bar; }
      template<typename type_t>
      bool operator()(const type_t &t) const { return t >= bar; }
    private:
      double bar;
    };
    
    
    template<typename predicate_t>
    class Not {
    public:
      Not(predicate_t p): predicate(p) { }
    
      template <typename type_t>
      bool operator()(const type_t &t) const {
        return !predicate(t);
      }
    
    private:
      predicate_t predicate;
    };
    
    template <class predicate_type>
    Not<predicate_type> Negate(predicate_type p)
    {
        return p;
    }
    
    #include <iostream>
    int main()
    {
        std::cout << Negate(HasTenFoo<double>)(11.0) << '\n';
        std::cout << Negate(HasEnoughFoo(13.0))(11.0) << '\n';
    }
    

    Some important notes:

    Not’s constructor uses the initialization list. This removes the requirement that the predicate type has a default constructor (which HasEnoughFoo doesn’t have).

    You definitely don’t want to mess with pointers to predicates. Function objects are supposed to be light-weight objects that can be copied without a worry.

    Because Not is a template class, with a potentially complicated template argument, but you’d normally just use it as a temporary (as an unnamed argument to a function taking a predicate), add a template function that deduces the complicated type for you (trick used all over the standard library) – here Negate.

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

Sidebar

Related Questions

No related questions found

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.