I want to write a concept that describes function objects that are not only invocable but, like std::function
, have an "empty" state that is testable via contextual conversion to bool
.
The boolean-testable exposition-only concept sounds like it might be appropriate but, since it is written in terms of convertible_to
, requires that its argument be implicitly convertible to bool:
template<class T> concept BooleanTestable = std::convertible_to<T, bool>;
static_assert(BooleanTestable<void(*)()>); // ok
static_assert(BooleanTestable<std::function<void()>>); // fails :(
Is there something in the Standard <concepts>
library that can help, or do I need to write a requires-expression myself, and if so, which form should I use? The definition of contextually converted to bool
is expressed in the form of a declaration-statement (i.e., bool t(E);
), but requires-expressions can only check expressions:
template<class T> concept ContextuallyConvertibleToBool = requires (T x) {
bool(x); // constructor cast?
static_cast<bool>(x); // or static_cast?
x ? void() : void(); // or something a little stranger?
};
Considering subsumption, readability and maintenance, I'd prefer to use a form that has precedent.