Thread: Predicate definition

  1. #1
    Registered User
    Join Date
    Oct 2002
    Posts
    291

    Predicate definition

    Does the C++ standard define an interface for a predicate?

    The reason I am asking is that I have an template class (see below) where I would like to accept a predicate as an input to the constructor. How do I specify a predicate as an input argument to the constructor? I have read online that the unary_function<> base class in the standard is not intended to be an interface.

    Code:
    template<typename T> 
    Class Myfoo
    {
      Myfoo();
      ...	
    }
    One example I have seen online somewhere is this;
    Code:
    template<typename T, typename Pred>
    Class Myfoo
    {
      Myfoo(Pred p);
      ...	
    }
    but I dont really need another generic type, I want a more specific type, a predicate.

    Any suggestions?

  2. #2
    Registered User
    Join Date
    Oct 2006
    Posts
    3,445
    if you know that all your predicates will have the same signature (parameter count and types), then you can use a typedef like so:

    Code:
    typedef void Pred(int param1, double param2);

  3. #3
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    There was this effort to standardise concepts that I believe started from the Boost.ConceptCheck library, but it has been delayed to some future edition of the standard.

    Anyway, the normal course of things is to do something like your second code snippet, then just use the predicate and let an error happen if it does not follow the prescribed interface. This way it does not have to matter if the user passes a function pointer or a function object as the predicate.

    Alternatively, you could define an abstract base class to serve as the predicate interface, but that may just be an unnecessary limitation (and possible virtual function call overhead).
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  4. #4
    The larch
    Join Date
    May 2006
    Posts
    3,573
    If you only want to use the predicate in the constructor, you can make the constructor a template.

    Code:
    template<typename T>
    class Myfoo
    {
    public:
        template <class Pred>
        Myfoo(Pred p);
    };
    I might be wrong.

    Thank you, anon. You sure know how to recognize different types of trees from quite a long way away.
    Quoted more than 1000 times (I hope).

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Definition of myself
    By ahmed el sakka in forum C++ Programming
    Replies: 6
    Last Post: 04-26-2011, 11:33 AM
  2. Multiple definition error, one definition
    By frog in forum C++ Programming
    Replies: 9
    Last Post: 10-21-2010, 03:15 AM
  3. PREDICATE( name, arity )
    By HJoshy in forum General AI Programming
    Replies: 1
    Last Post: 06-05-2010, 01:16 PM
  4. Definition?
    By reRanger in forum C++ Programming
    Replies: 6
    Last Post: 11-23-2004, 05:36 PM
  5. key input definition
    By hkmixxa in forum C++ Programming
    Replies: 3
    Last Post: 08-09-2004, 05:39 PM