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
};
}
static_assertfor whatever you want it to be called.std::is_trivially_copyableon the lambda type itself.std::is_trivially_copyablein user C++ by the way. That requires magic compiler support. And having a trivial copy operation is not the same as being trivially copyable, which is what is required for memcpy.std::is_trivially_copyable_v<decltype(lambda2)>istruethen the lambda does not have members that are not trivially copyablestd::is_trivially_copyable.