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

  • Home
  • SEARCH
  • 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 141555
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 11, 20262026-05-11T07:47:43+00:00 2026-05-11T07:47:43+00:00

I need to designe predicate for stl algorithms such as find_if, count_if. namespace lib

  • 0

I need to designe predicate for stl algorithms such as find_if, count_if.

namespace lib {     struct Finder     {         Finder( const std::string& name ):             name_( name )         {         }          template< typename TElement >         bool operator( const TElement& element )         {             return element.isPresent( name_ );         }          /* template< typename TElement >         bool operator( const TElement& element )         {             const Data& data = element.getData();             return data.isPresent( name_ );         }*/      }; } 

But I need it to have different operators () according to presence of some certain methods in TElement. Like if it has ‘getData’ I’d like to check that data and if it hasn’t I’d do some other actions.

I am aware of SFINAE. But I don’t have boost:: on the project. So either there is some easy implementation of template ‘has_method’ or you know some other design solution.

I can’t point specific types and simply overload because I’d like to put this Predicate to the one of the project library, which don’t know about those specific classes with ‘getData’ method.

Solution with class traits are good as far as there is no namespaces. Predicate Finder in in ‘lib’ namespace and class with ‘getData’ is in ‘program’ namespace.

Thanks.

  • 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. 2026-05-11T07:47:44+00:00Added an answer on May 11, 2026 at 7:47 am

    Why use template mathods at all? Just use the specific class that you want to base it on or a common base classes if there are lots of class types.

    e.g.

    struct Finder {     Finder( const std::string& name ):         name_( name )     {     }      bool operator( const IsPresentBaseClass& element )     {         return element.isPresent( name_ );     }      bool operator( const GetDataBaseClass& element )     {         const Data& data = element.getData();         return data.isPresent( name_ );     }   }; 

    If this pattern happens a lot with different class types and you know the types before using the predicate you could template the predicate itself.

    e.g.

    template<class T1, class T2> struct Finder {     Finder( const std::string& name ):         name_( name )     {     }      bool operator( const T1& element )     {         return element.isPresent( name_ );     }      bool operator( const T2& element )     {         const Data& data = element.getData();         return data.isPresent( name_ );     }   }; 

    Or another approach you could use is to use some sort of class traits to hold the information.

    e.g.

    struct UseIsPresent {     template<class T>     static bool CompareElement( const T& element, const std::string& name )     {         return element.isPresent( name );     } };  struct UseGetData {     template<class T>     static bool CompareElement( const T& element, const std::string& name )     {         const Data& data = element.getData();         return data.isPresent( name );     }  };  // default to using the isPresent method template <class T> struct FinderTraits {     typedef UseIsPresent FinderMethodType; };  // either list the classes that use GetData method // or use a common base class type, e.g. UseGetData template <> struct FinderTraits<UseGetData> {     typedef UseGetData FinderMethodType; };  struct Finder {     Finder( const std::string& name )     : name_( name )     {     }      template<class T>     bool operator()( const T& element )     {         return FinderTraits<T>::FinderMethodType::CompareElement<T>(element, name_);     }      std::string name_; }; 

    The downsides of all these methods is that at some point you need to know the types to be able to split them up into which method to use.

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

Sidebar

Ask A Question

Stats

  • Questions 106k
  • Answers 106k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer Apparently not. You can group three RadioButtons together using a… May 11, 2026 at 8:53 pm
  • Editorial Team
    Editorial Team added an answer DELIMITER $$ DROP PROCEDURE IF EXISTS `DeleteDataByTransactionID` $$ CREATE DEFINER=`root`@`%`… May 11, 2026 at 8:53 pm
  • Editorial Team
    Editorial Team added an answer You can use the CustomLog directive to pipe the access… May 11, 2026 at 8:53 pm

Related Questions

OK I need to design a way to keep track of how many of
I am using an interface called Predicate which is used for sifting through Collections.
I am trying to teach myself Python by working through some problems I came
I need to design a small project for generating excel reports in .NET, which

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

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.