Since C++ 20, the concepts have been released to constrain templates and auto.
I wanted to create a concept that only defines a lambda, is it possible?
template <typename T>
concept lambda = /* ... */ ;
And then I could apply like this:
int add(int x, int y) {
return x + y;
}
lambda auto func1 = []{ return 5; }; // constrain satisfied
lambda auto func2 = add; // constrain unsatisfied, compilation error.
You can get the type name of T at compiler time, and then determine whether it contains the mangled name of lambda, which starts with <lambda in gcc and (lambda in clang:
Although it is feasible, it is actually not recommended.
#include <string_view>
template <typename T>
consteval bool is_lambda() {
std::string_view name = __PRETTY_FUNCTION__;
auto pos = name.find("T = ");
name.remove_prefix(pos + 4);
if (pos = name.rfind("::"); pos != name.npos)
name.remove_prefix(pos + 2);
#ifdef __clang__
return name.starts_with("(lambda");
#elif defined(__GNUC__)
return name.starts_with("<lambda");
#endif
}
template <class T>
concept lambda = is_lambda<T>();
Demo.
I don't know why you'd want this, but the closest I can think of is this:
template <typename T>
concept lambda = requires { &T::operator(); };
Compiler Explorer link
Basically, this requires that T has a call operator. This works because that's how lambdas are implemented: they give you an object whose type has a compiler generated operator().
However, this concept accepts things that are not lambdas, namely user defined structs that have call operators, such as std::function, std::bind, std::reference_wrapper, etc. Thus, this concept accepts basically all callables which are not function pointers, which is a strange categorization of C++ function objects.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With