How can one call static_assert for each data member of any given C++ lambda?
What I'm trying to do is roll my own memcpy-able std::function and have realized that any lambdas must have trivially copyable data members. I'd like to put some assertions in place for this.
Example:
template<typename T> struct has_trivial_copy {enum {value = false};};
template<> struct has_trivial_copy<int> {enum {value = true};};
int main() {
int i = 0;
std::string str;
auto lambda1 = [i] {
// static_assert(has_trivial_copy<each data member...>::value); // okay
};
auto lambda2 = [i, str] {
// static_assert(has_trivial_copy<each data member...>::value); // fail
};
}
While it's not possible to call
static_assertfor every member of a lambda, it is possible to usestd::is_trivially_copyableon any given lambda to assert that it is trivially copyable.I've added the following
static_assertto my customstd::functionand it works nicely to show me all the places where I'm using non-trivially copyable data members in lambdas: