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
| #include <iostream>
#include <functional>
class A {
public:
void virtual routine() {}
void executer(std::function<void(int)> fct, int param) {
fct(param);
}
void executer2(std::function<void(void)> fct) {
fct();
}
template <class T>
void executer3(void (T::*fct)(int), int param) {
static_assert(std::is_base_of<A, T>::value, "nop.png");
(static_cast<T*>(this)->*fct)(param);
}
};
class B : public A {
public:
void routine() {
executer([this](int i) { ma_fct(i); }, 1);
executer(std::bind(&B::ma_fct, this, std::placeholders::_1), 2);
executer2(std::bind(&B::ma_fct, this, 3));
executer3(&B::ma_fct, 4);
}
void ma_fct(int p) {
std::cout << p << std::endl;
}
};
int main() {
B b;
b.routine();
return 0;
} |
Partager