Date: Wed, 03 Dec 2025 20:34:23 +0300
Let’s assume that we have an algorithm with two predicates. One is an unary predicate and other is a binary predicate.
The unary predicate serves to find a starting element in a sequence from which the algorithm processes each pair of adjacent elements that satisfy the condition of the binary predicate. When some pair of adjacent elements does not satisfy the condition then the algorithm searches a next element that satisfy the condition of the unary predicate and so on.
Now let’s we want to call the algorithm for any starting element. That is the unary predicate shall return true for any element of a sequence. It is only important that adjacent elements would satisfy the condition of the binary predicate.
In such a case we could use a lambda expression like that
[]( const auto & ) { return true; }
as the unary predicate.
But code would look more readable if the name of the used predicate would say what it is doing.
I suggest to include in the Standard unary predicates: true_predicate and false_predicate.
For example
template <typename T = void>
struct true_predicate
{
constexpr bool operator ()( const T & ) const
{
return true;
}
};
template <>
struct true_predicate<void>
{
template <typename T>
constexpr bool operator ()( T && ) const
{
return true;
}
};
template <typename T = void>
struct false_predicate
{
constexpr bool operator ()( const T & ) const
{
return false;
}
};
template <>
struct false_predicate<void>
{
template<class T>
constexpr bool operator()( T && ) const
{
return false;
}
};
With best regards,
Vlad from Moscow
The unary predicate serves to find a starting element in a sequence from which the algorithm processes each pair of adjacent elements that satisfy the condition of the binary predicate. When some pair of adjacent elements does not satisfy the condition then the algorithm searches a next element that satisfy the condition of the unary predicate and so on.
Now let’s we want to call the algorithm for any starting element. That is the unary predicate shall return true for any element of a sequence. It is only important that adjacent elements would satisfy the condition of the binary predicate.
In such a case we could use a lambda expression like that
[]( const auto & ) { return true; }
as the unary predicate.
But code would look more readable if the name of the used predicate would say what it is doing.
I suggest to include in the Standard unary predicates: true_predicate and false_predicate.
For example
template <typename T = void>
struct true_predicate
{
constexpr bool operator ()( const T & ) const
{
return true;
}
};
template <>
struct true_predicate<void>
{
template <typename T>
constexpr bool operator ()( T && ) const
{
return true;
}
};
template <typename T = void>
struct false_predicate
{
constexpr bool operator ()( const T & ) const
{
return false;
}
};
template <>
struct false_predicate<void>
{
template<class T>
constexpr bool operator()( T && ) const
{
return false;
}
};
With best regards,
Vlad from Moscow
Received on 2025-12-03 17:34:57
