I want to implement a template function which takes a lambda as argument.
#include <functional>
template<typename ... Result> using Fun = std::function<void(Result ...)>;
template<typename ... Result> void yield(Fun<Result ...>&& body) {};
template <typename T>
struct identity {
typedef T type;
};
template<typename ... Result> void yield2(typename identity<Fun<Result ...>>::type && body) {};
int main() {
yield<char>(
Fun<char>(
[](char) -> void {} // 1. success
)
);
yield2<char>(
[](char) -> void {} // 2. success with identify
);
yield<char>(
[](char) -> void {} // 3. fail, seems achievable
);
yield(
[](char) -> void {} // 4. fail, impossible ?
);
return 0;
}
Why case 3 fails ? I already giving the template parameters to the template, so it should be able to deduce the function type and implicit convert the lambda to function
EDIT:
Compiler always reduce template parameters from function arguments, can we reverse it ?
yield<char>(
[](auto&& c) -> void {} // 5. is it possible ?
);
Aucun commentaire:
Enregistrer un commentaire