how to deduce a type is lambda ? like std::is_function
? I've also tried std::is_invocable
, but it need exactly types of parameters.

- 33
- 6
-
You can check whether it is invokable with specific parameter types. There are arcane techniques for checking whether it has *any* `operator()`, but this is usually not a valid thing to check. In any case, you can't check whether it's specifically a lambda type. – Brian Bi Oct 02 '21 at 00:37
-
@BrianBi, I think that is sad but true :(. I tried many ways... – VeNToR Oct 02 '21 at 00:41
-
3It's just an indication that you need to rethink the broader problem you are trying to solve, and find another way that doesn't depend on knowing whether a type is a lambda. – Brian Bi Oct 02 '21 at 00:43
-
2Lambdas *are not special* in C++. There is never any reason to write a function that can *only* take a lambda. Functions should accept any particular callable object appropriate to its use. – Nicol Bolas Oct 02 '21 at 00:43
-
2@BrianBi: Yeah, by my understanding a lambda is syntactic sugar for a standard functor class which a user could write by hand (the captures are constructor arguments, the arguments are the arguments to `operator()`); at best, with RTTI, you might be able to do terrible heuristics to figure out if the underlying name is something the cat spat up (probably a compiler generated name for the lambda) vs. a vaguely human name (though with the name-mangling compilers apply, it's going to look a lot like cat sick too). But even if you can, and even if heuristics work, there's never a reason to do it. – ShadowRanger Oct 02 '21 at 00:44
-
3@VeNToR: I strongly suspect this is [an XY problem](https://meta.stackexchange.com/q/66377/322040). *Why* do you think you need to do this? What problem are you solving? – ShadowRanger Oct 02 '21 at 00:46
-
@ShadowRanger: my code is very long to share it. I wish I could share ? – VeNToR Oct 02 '21 at 00:49
-
2@VeNToR: I'm not asking for all your code. Just an explanation for why you think it's important to *specifically* identify lambdas and handle them differently from other functors, which they are drop-in equivalents for. The explanation could lead to an answer that solves your actual problem; the whole point of XY problems is that you've settled on a bad solution to your real problem, and you're trying to make the bad solution work, neglecting to describe the real problem that has a good solution. – ShadowRanger Oct 02 '21 at 00:52
-
With https://stackoverflow.com/a/56766138/2193968 you can do https://godbolt.org/z/G9dWrMaW4 – Jerry Jeremiah Oct 03 '21 at 22:52
-
@JerryJeremiah, thanks but this uses runtime processing... – VeNToR Oct 04 '21 at 19:35
-
1@VeNToR Well, since string_view doesn't have contains() until C++23 what about this: https://godbolt.org/z/d39Ed69nW – Jerry Jeremiah Oct 05 '21 at 02:08
1 Answers
You cannot check for every invocable types without having the exact parameters. Take this for example:
auto my_lambda = [](std::integral auto number) {};
There are few properties that make it hard to check without knowing the parameter types:
- You cannot take the address of
operator()
since it's a template. You could take the address ofoperator()<int>
but you'd have to know the type in advance - You cannot try the lambda with a "convert to anything" type, since it's not an integral
- There are no other observable properties of the type that can be observed.
That makes it really hard to have a is_lambda
trait. Also, you could have something like that:
struct A {
void operator()(int) const {}
};
This would be indistinguishable from a lambda.
There are however, very arcane ways to check if a type is a lambda or not.
I would strongly recommend not to do that.
You could theorically check the name of the type by using __PRETTY_FUNCTION
or __FUNCSIG__
, and check if the name look like a compiler generated name for a lambda, but that would exclude any other user defined callable types, which are, and should be accepted like lambda.
I leave the implementation as an exercise to the reader.

- 39,621
- 9
- 77
- 141