Predicate definition

This is a discussion on Predicate definition within the C++ Programming forums, part of the General Programming Boards category; Does the C++ standard define an interface for a predicate? The reason I am asking is that I have an ...

  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
    2,584
    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
    22,265
    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).
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    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, 12:33 PM
  2. Multiple definition error, one definition
    By frog in forum C++ Programming
    Replies: 9
    Last Post: 10-21-2010, 04:15 AM
  3. PREDICATE( name, arity )
    By HJoshy in forum General AI Programming
    Replies: 1
    Last Post: 06-05-2010, 02: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, 06:39 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21