| 1 |
#include <iostream> |
| 2 |
#include <utility> |
| 3 |
#include <functional> |
| 4 |
|
| 5 |
#include <vector> |
| 6 |
#include <string> |
| 7 |
#include <stack> |
| 8 |
#include <thread> |
| 9 |
|
| 10 |
using namespace std; |
| 11 |
|
| 12 |
struct Forwarder |
| 13 |
{ |
| 14 |
string d_cl; |
| 15 |
|
| 16 |
Forwarder() |
| 17 |
: |
| 18 |
d_cl("1234567890") |
| 19 |
{} |
| 20 |
template<typename ...Args> |
| 21 |
string substr(Args &&...args) |
| 22 |
{ |
| 23 |
return d_cl.substr(std::forward<Args>(args)...); |
| 24 |
} |
| 25 |
|
| 26 |
//// void fwd(size_t (std::string::*ptr)()) |
| 27 |
|
| 28 |
//////////////////////////////////////////////////////////////// |
| 29 |
// template <typename Ret, typename Member, typename Arg> |
| 30 |
// Ret fwd(Member member, Arg arg) |
| 31 |
// |
| 32 |
//// decltype (static_cast<Client &>( |
| 33 |
//// *static_cast<Client *>(0) |
| 34 |
//// )) |
| 35 |
// |
| 36 |
// |
| 37 |
//// decltype(s_r) |
| 38 |
// |
| 39 |
//// Niet met .* decltype ((d_cl.*member)(arg)) |
| 40 |
// |
| 41 |
// { |
| 42 |
// return (d_cl.*member)(arg); |
| 43 |
// } |
| 44 |
//////////////////////////////////////////////////////////////// |
| 45 |
|
| 46 |
// template <typename Ret, typename Member, typename ...Args> |
| 47 |
// Ret fwd(Member member, Args&&...args) |
| 48 |
// { |
| 49 |
// return (d_cl.*member)(std::forward<Args>(args)...); |
| 50 |
// } |
| 51 |
}; |
| 52 |
|
| 53 |
|
| 54 |
struct X |
| 55 |
{ |
| 56 |
template <typename T>//, typename ...Args> |
| 57 |
struct result |
| 58 |
{ |
| 59 |
using type = void; |
| 60 |
}; |
| 61 |
|
| 62 |
void operator()(int x) |
| 63 |
{ |
| 64 |
cout << "int\n"; |
| 65 |
} |
| 66 |
void operator()(double x) |
| 67 |
{ |
| 68 |
cout << "double\n"; |
| 69 |
} |
| 70 |
void operator()(double x, char const *) |
| 71 |
{ |
| 72 |
cout << "double, string\n"; |
| 73 |
} |
| 74 |
void operator()(ostream &out, double &x, char const *) |
| 75 |
{ |
| 76 |
cout << "double, string\n"; |
| 77 |
} |
| 78 |
}; |
| 79 |
|
| 80 |
struct One |
| 81 |
{ |
| 82 |
One() |
| 83 |
{ |
| 84 |
cout << "One()\n"; |
| 85 |
} |
| 86 |
One(int) |
| 87 |
{ |
| 88 |
cout << "One(int)\n"; |
| 89 |
} |
| 90 |
}; |
| 91 |
|
| 92 |
struct Two |
| 93 |
{ |
| 94 |
Two() |
| 95 |
{ |
| 96 |
cout << "Two()\n"; |
| 97 |
} |
| 98 |
Two(int) |
| 99 |
{ |
| 100 |
cout << "Two(int)\n"; |
| 101 |
} |
| 102 |
}; |
| 103 |
|
| 104 |
template <typename Class, typename ...Args> |
| 105 |
Class factory(Args &&...args) |
| 106 |
{ |
| 107 |
return Class(std::forward<Args>(args)...); |
| 108 |
} |
| 109 |
|
| 110 |
void calledFunction(ostream &out) |
| 111 |
{ |
| 112 |
out << "hello\n"; |
| 113 |
} |
| 114 |
|
| 115 |
void calledFunction(int x) |
| 116 |
{ |
| 117 |
++x; |
| 118 |
} |
| 119 |
|
| 120 |
void calledFunction(int &x) |
| 121 |
{ |
| 122 |
++x; |
| 123 |
} |
| 124 |
|
| 125 |
template <typename ...Args> |
| 126 |
void caller(Args &&...args) |
| 127 |
{ |
| 128 |
calledFunction(std::forward<Args>(args) ...); |
| 129 |
} |
| 130 |
|
| 131 |
int main() |
| 132 |
{ |
| 133 |
int x = 0; |
| 134 |
caller(cout); |
| 135 |
caller(x); |
| 136 |
cout << x << '\n'; |
| 137 |
|
| 138 |
// int arg; |
| 139 |
// X x; |
| 140 |
// Forwarder forwarder; |
| 141 |
// |
| 142 |
// string &&out = forwarder.substr(5, 2); |
| 143 |
// |
| 144 |
// factory<One>(); |
| 145 |
// Two two(factory<Two>(3)); |
| 146 |
// |
| 147 |
// factory<Two>(two); |
| 148 |
// |
| 149 |
// cout << out << '\n'; |
| 150 |
|
| 151 |
// forwarder.function(x, 0); |
| 152 |
// forwarder.function(x, 0.0); |
| 153 |
// |
| 154 |
// double d; |
| 155 |
// thread t(x, ref(cout), ref(d), "hi"); |
| 156 |
} |