Last active
January 3, 2024 13:26
-
-
Save XueshiQiao/1e7a69ac0b2da423b36a4625d354c068 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <iostream> | |
#include <functional> | |
using namespace std; | |
// -std=c++17 | |
template <typename In, typename Out> | |
class Filter { | |
public: | |
using Fn = std::function<Out(In)>; | |
Filter(Fn fn) : fn_(std::move(fn)) {} | |
template <typename In_> | |
Out Process(In_ value) { | |
static_assert(std::is_same_v<In_, In_>, "type must match"); | |
return fn_(std::forward<In_>(value)); | |
} | |
private: | |
Fn fn_; | |
}; | |
int main(int argc, char *argv[]) { | |
{ | |
auto filter = Filter<int32_t, std::string>([](int32_t value) -> std::string { | |
return std::to_string(value); | |
}); | |
std::cout << "filter: " << filter.Process(100) << std::endl; | |
} | |
{ | |
// Bad case 1, auto deduction fail | |
// // no viable constructor or deduction guide for deduction of template arguments of 'Filter' | |
// auto filter = Filter([](int32_t value) -> std::string { | |
// return std::to_string(value); | |
// }); | |
// std::cout << "filter: " << filter.Process(100) << std::endl; | |
} | |
{ | |
std::function<std::string(int32_t)> func = [](int32_t value) { | |
return std::to_string(value); | |
}; | |
// auto deduction works, Filter<int32_t, std::string> is not need, Filter is Ok | |
auto filter = Filter(func); | |
std::cout << "filter: " << filter.Process(100) << std::endl; | |
} | |
{ | |
//Bad case 2, auto deduction fail | |
// auto func = [](int32_t value) -> std::string{ | |
// return std::to_string(value); | |
// }; | |
// // no viable constructor or deduction guide for deduction of template arguments of 'Filter' | |
// auto filter = Filter(func); | |
// std::cout << "filter: " << filter.Process(100) << std::endl; | |
} | |
return 0; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Solution