I'm using C++20 & I'm wondering if Concepts is at all possible to solve a situation like this.
Let's say I have a function:
template <typename F>
void doSomething(F f) {
f();
}
doSomething
accepts a callable objects (technically lambdas but shouldn't matter I think) & I want to ensure that no member variables of F
have a type T
. For example, I want:
BadType t;
int x = ...;
double y = ...;
doSomething([t = kj::mv(t), x, y] { ... }); // I want a compile error
doSomething([x, y] { ... }); // No error.
Similarly, I want to validate the arguments of the callable in a similar way:
doSomething([x, y](BadType t) { ... }); // I want a compile error
doSomething([x, y](std::vector<int> c) { ... }); // No compile error
I don't know if this complicates things, but technically BadType
itself is a templated type (I want to disable all instances regardless of the template value).
An acceptable answer could be one that provides an example, but I'm also happy with good tutorials that someone feels I should be could be able to cobble together to accomplish. I'm very experienced with C++ & template meta-programming, but concepts feel like a totally alien thing at the moment. Of course, if this isn't possible, then happy to accept such an answer too.
This feels like something that the Reflection TS would be perfect for, but alas clang doesn't have this in an accessible way even in v13 (& I think similarly the GCC work is in a non-mainline branch still). I've explored various static reflection libraries for C++17, but they all suffer from needing to modify the type, which isn't possible here since I'm introspecting a lambda (and BadType
is a type defined in a 3p library although that shouldn't matter).
I suspect the answer must be no since each lambda I pass in will have an arbitrary set of names for the variables that get captured & the only examples I've seen of concepts trying to enforce the type of a member variable require a single known variable name, but maybe this is an interesting challenge for someone who's a Concepts master.