I know that a lambda
object is not a std::function
object, so I understand that this would not work:
template <typename ...args_t>
void func(std::function<void(args_t...)> function_, args_t ...args){
/// do something here
}
void test() {
func([](int a){ }, 1);
}
but why would it work if I add a type_identity to wrap it?
template <typename T>
struct type_identity {
using type = T;
};
template <typename T>
using type_identity_t = typename type_identity<T>::type;
template <typename... args_t>
void func_wrapped(type_identity_t<std::function<void(args_t...)>> function_,
args_t ...args) {
static_assert(std::is_same< std::function<void(args_t...)>,
type_identity_t<std::function<void(args_t...)>>
>::value,
"different type");
/// do something here
}
void test() {
func_wrapped([](int a){ }, 1);
}
as far as I can see, these two look pretty much the same, and it even passed the static_assert
which means they are the same to std::is_same
.
but the compiler just doesn't think so. it told me that the in former code, lambda cannot match any function, while the latter one can.
error: no matching function for call to ‘func(test()::<lambda(int)>, int)’
so, my question is: why do they behave differently? what did type_identity do implicitly?