Coder Social home page Coder Social logo

Comments (5)

vG5YtZQN avatar vG5YtZQN commented on August 15, 2024 4
#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);
}

from threadpool.

fogti avatar fogti commented on August 15, 2024 1

With C++11 the following should be possible...

std::shared_ptr<A> a = std::make_shared<B>();
tp.enqueue([a]() { a->a(); });

from threadpool.

z80sui avatar z80sui commented on August 15, 2024

@progschj

from threadpool.

z80sui avatar z80sui commented on August 15, 2024

@wilx please have a look

from threadpool.

vG5YtZQN avatar vG5YtZQN commented on August 15, 2024

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);
}

from threadpool.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.