I just read somebody call a class with a constructor and an operator() a predicate:
// Example
class Foo {
public:
Foo(Bar);
bool operator()(Baz);
private:
Bar bar;
};
However, I haven’t heard the word predicate being used in this context before. I would call such a thing a functor. For me, a predicate would be something from the domain of formal logic.
This raises the following questions:
- Is this a common word for something like
Foo? - Are both terms used interchangeably, or do they mean slightly different things?
- Or
- Does the return type (
boolversus something else) have something to do with it? - What about the
operator()beingconst?
- Does the return type (
Functor is a term that refers to an entity that supports operator
()in expressions (with zero or more parameters), i.e. something that syntactically behaves as a function. Functor is not necessarily an object of some class with overloadedoperator (). Ordinary function names are functors as well. Although in some contexts you can see the term “functor” used in a more narrow and exclusive sense: just class objects, but not ordinary functions.A predicate is a specific kind of functor: a functor that evaluates to a boolean value. It is not necessarily a value of
booltype, but rather a value of any type with “boolean” semantics. The type should be implicitly convertible toboolthough.