#include
#include
#include
#include
template <typename Callable, typename... Args>
auto deduce_return_type(Callable&& c, Args&&... args)
-> typename std::result_of<Callable(Args...)>::type {
return std::forward<Callable>(c)(std::forward<Args>(args)...);
}
template<typename F, typename... Args>
auto binddd(F&& f, Args&&... args) -> std::function<decltype(deduce_return_type(f, args...))()> {
using ReturnType = decltype(deduce_return_type(f, args...));
std::function<ReturnType()> func = std::bind(std::forward<F>(f), std::forward<Args>(args)...);
return func;
}
template<typename F, typename... Args>
auto binddd2(F&& f, Args&&... args) -> std::function<decltype(f( args...))()> {
std::function<decltype(f(args...))()> func = std::bind(std::forward<F>(f), std::forward<Args>(args)...);
return func;
}
template<typename F, typename... Args>
auto binddd3(F&& f, Args&&... args){
auto func = std::bind(std::forward<F>(f), std::forward<Args>(args)...);
return func;
}
void Function2(int* value) {
*value = *value + 1;
std::cout << "value" << *value << "\n";
}
class MyClass {
public:
void myMemberFunction(int* value) {
*value = *value + 1;
std::cout << "Value from thread: " << *value << std::endl;
}
};
int main() {
int* v = new int(1);
MyClass myclass;
{
auto callable = std::bind(Function2, v);
callable();
auto callable2 = std::bind(&MyClass::myMemberFunction, &myclass,v);
callable2();
}
{
auto callable = binddd(Function2, v);
callable();
auto callable2 = binddd(&MyClass::myMemberFunction, &myclass, v);
callable2();
}
{
auto callable = binddd2(Function2, v);
callable();
auto callable2 = binddd2(&MyClass::myMemberFunction, &myclass, v);
callable2();
}
{
auto callable = binddd3(Function2, v);
callable();
auto callable2 = binddd3(&MyClass::myMemberFunction, &myclass, v);
callable2();
}
delete v;
return 0;
}
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
- 24
- 25
- 26
- 27
- 28
- 29
- 30
- 31
- 32
- 33
- 34
- 35
- 36
- 37
- 38
- 39
- 40
- 41
- 42
- 43
- 44
- 45
- 46
- 47
- 48
- 49
- 50
- 51
- 52
- 53
- 54
- 55
- 56
- 57
- 58
- 59
- 60
- 61
- 62
- 63
- 64
- 65
- 66
- 67
- 68
- 69
- 70
- 71
- 72
- 73
- 74
- 75
- 76
- 77
- 78
- 79
- 80
- 81
- 82
- 83
- 84
- 85
- 86
- 87
- 88
- 89
- 90
- 91