| 1 |
#include <iostream> |
| 2 |
#include <string> |
| 3 |
#include <functional> |
| 4 |
|
| 5 |
using namespace std; |
| 6 |
|
| 7 |
template <typename Class> |
| 8 |
class Filter |
| 9 |
{ |
| 10 |
Class obj; |
| 11 |
public: |
| 12 |
template <typename Arg> |
| 13 |
std::invoke_result<Class, Arg>::type operator()(Arg const &arg) const |
| 14 |
{ |
| 15 |
return obj(arg); |
| 16 |
} |
| 17 |
}; |
| 18 |
|
| 19 |
|
| 20 |
struct Convert // if there's just one ReturnType operator()(...), simply |
| 21 |
{ // specify `using type = ReturnType' |
| 22 |
template <typename Signature> |
| 23 |
struct result; |
| 24 |
|
| 25 |
double operator()(int x) const |
| 26 |
{ |
| 27 |
return 12.5; |
| 28 |
} |
| 29 |
template <typename Class> |
| 30 |
struct result<Class(int)> |
| 31 |
{ |
| 32 |
using type = double; |
| 33 |
}; |
| 34 |
|
| 35 |
string operator()(double x) const |
| 36 |
{ |
| 37 |
return "hello world"s; |
| 38 |
} |
| 39 |
template <typename Class> |
| 40 |
struct result<Class(double)> |
| 41 |
{ |
| 42 |
using type = string; |
| 43 |
}; |
| 44 |
}; |
| 45 |
|
| 46 |
int main() |
| 47 |
{ |
| 48 |
Filter<Convert> fc; |
| 49 |
|
| 50 |
cout << fc(5.5).length() << '\n'; |
| 51 |
cout << fc(5) << '\n'; |
| 52 |
} |