Open z80sui opened 5 years ago
@progschj
@wilx please have a look
With C++11 the following should be possible...
std::shared_ptr<A> a = std::make_shared<B>();
tp.enqueue([a]() { a->a(); });
#include <iostream>
template<typename Ft, class Cp, class ...Args>
void enqueue(Ft &&ft, Cp &&cp, Args &&...args) {
(cp.*ft)(args...);
}
class A {
public:
virtual void a() = 0;
};
class B : public A {
void a() override {
std::cout << "B called" << std::endl;
}
};
class C : public A {
void a() override {
std::cout << "C called" << std::endl;
}
};
int main() {
A *a1 = new B{};
A *a2 = new C{};
enqueue(&A::a, *a1);
enqueue(&A::a, *a2);
}
if you would like a more ugly version,take a look like this...
#include <iostream>
#include <functional>
template<bool, typename Ft, class ...Args>
struct call_member_function_impl;
template<typename Ft, class ...Args>
struct call_member_function_impl<false, Ft, Args...> {
public:
static void call(Ft &&ft, Args &&...args) {
ft(args...);
}
};
template<typename Ft, class ...Args>
struct call_member_function_impl<true, Ft, Args...> {
public:
template<typename Caller, typename Ins, class ...Parms>
static void call(Caller &&ft, Ins &&ins, Parms &&...args) {
(ins.*ft)(args...);
}
};
template<typename Ft, class ...Args>
void call_member_function_if_possible(Ft &&ft, Args &&...args) {
typedef call_member_function_impl<std::is_member_function_pointer<Ft>::value, Ft, Args...> type;
type::template call<Ft, Args...>(std::forward<Ft>(ft), std::forward<Args>(args)...);
};
class A {
public:
virtual void a() = 0;
};
class B : public A {
void a() override {
std::cout << "B called" << std::endl;
}
};
class C : public A {
void a() override {
std::cout << "C called" << std::endl;
}
};
int main() {
A *a1 = new B{};
A *a2 = new C{};
call_member_function_if_possible(&A::a, *a1);
call_member_function_if_possible(&A::a, *a2);
}
create a lambda ?
@Smalldy see #66 (comment) 看 #66 (comment)
yes that is
class A { virture void a() = 0; }; class B:public A { void a(){} override; }; how can I use override function a like this? A a = new B(); enqueue( &a->a, this)