添加到队列模板函数C++

发布于 2025-02-05 00:27:22 字数 2102 浏览 3 评论 0原文

我在Google上搜索如何存储和执行函数(模板函数)传递在队列中,但我没有找到足够的好答案...

这是我的

窗口

struct QueueEventFunction {

    std::vector<std::function<void()>> v;  // stores the functions and arguments
                                           // as zero argument lambdas

    template <typename F,/*template <typename U, typename = std::allocator<U> >*/ typename ...Args>
    void Enqueue(F (*f), Args... args)
    {
        v.push_back([=] { f(args...); });  // add function and arguments
                                           // as zero argument lambdas
                                           // that capture the function and arguments
    }

    void CallAll()
    {
        for (auto f : v)
            f();                              // calls all the functions
    }
};

class Window : public sf::RenderWindow {
public:
    Window(sf::VideoMode window, const std::string& title, sf::Uint32 style = 7U, sf::ContextSettings settings = sf::ContextSettings());
    ~Window();
    template <typename F, /*template <typename ...U > */typename ...Args>
    void addQueue(F (*f), Args...args);
    void execQueue();
    
private:
    QueueEventFunction queue;
}

template <typename F, /*template <typename ...U >*/ typename ...Args>
void Window::addQueue(F (*f), Args...args) {
    queue.Enqueue(f, std::forward<Args>(args)...);
}

void Window::execQueue() {
    queue.CallAll();
}

代码

template<typename T>
void add(T a, T b) {
    return a + b;
}

class foo{
public:
    template<typename T> T add( T a, T b){ return a+ b;}
};

int main() {
        Window window(sf::VideoMode(600, 600), "Test Window",7U,sf::ContextSettings());
        window.addQueue(add<int>,1,2);                  // doesn't work
        foo bar;
        window.addQueue(&foo::add<int>,1,2);    // doesn"t work
        window.addQueue(bar.add<int>(1,2));    // doesn"t work
        return 0;
    }

。 而且,如果我放置功能成员类,则根本无法使用。 (编译错误)

您是否有一个想法正确地使函数Addqueue&lt;&gt;与args?

i search on google how to store and to execute functions (template function or not) passing in queue but i didn't find an enough good answer...

this is my code

Window.h

struct QueueEventFunction {

    std::vector<std::function<void()>> v;  // stores the functions and arguments
                                           // as zero argument lambdas

    template <typename F,/*template <typename U, typename = std::allocator<U> >*/ typename ...Args>
    void Enqueue(F (*f), Args... args)
    {
        v.push_back([=] { f(args...); });  // add function and arguments
                                           // as zero argument lambdas
                                           // that capture the function and arguments
    }

    void CallAll()
    {
        for (auto f : v)
            f();                              // calls all the functions
    }
};

class Window : public sf::RenderWindow {
public:
    Window(sf::VideoMode window, const std::string& title, sf::Uint32 style = 7U, sf::ContextSettings settings = sf::ContextSettings());
    ~Window();
    template <typename F, /*template <typename ...U > */typename ...Args>
    void addQueue(F (*f), Args...args);
    void execQueue();
    
private:
    QueueEventFunction queue;
}

template <typename F, /*template <typename ...U >*/ typename ...Args>
void Window::addQueue(F (*f), Args...args) {
    queue.Enqueue(f, std::forward<Args>(args)...);
}

void Window::execQueue() {
    queue.CallAll();
}

TestWindow.cpp

template<typename T>
void add(T a, T b) {
    return a + b;
}

class foo{
public:
    template<typename T> T add( T a, T b){ return a+ b;}
};

int main() {
        Window window(sf::VideoMode(600, 600), "Test Window",7U,sf::ContextSettings());
        window.addQueue(add<int>,1,2);                  // doesn't work
        foo bar;
        window.addQueue(&foo::add<int>,1,2);    // doesn"t work
        window.addQueue(bar.add<int>(1,2));    // doesn"t work
        return 0;
    }

i got a external symbol error.
and if i put a function member class it simply doesn't work. (compilation error)

have you got an idea to correctly make the function addQueue<> with args ?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

凉城已无爱 2025-02-12 00:27:22

代码的最大问题是,您将功能指针作为指针

template <typename F, typename ...Args>
void Enqueue(F (*f), Args... args)

以及

template <typename F, typename ...Args>
void addQueue(F (*f), Args...args);

您真正想要的是什么

template <typename F, typename ...Args>
void Enqueue(F f, Args... args)

template <typename F, typename ...Args>
void addQueue(F f, Args...args);

然后您可以传递任何可叫的东西。然后,您可以将其与BIND,LAMBDAS,类成员功能,类静态成员功能和正常功能的结果一起使用。

然后,您可以像

template<typename T>
int add(T a, T b) {
    std::cout << "add(" << a << "," << b << ") -> " << a+b << "\n";
    return a + b;
}

class foo{
public:
    template<typename T>
    T add( T a, T b) {
        std::cout << "foo::add(" << a << "," << b << ") -> " << a+b << "\n";
        return a + b;
    }

    template<typename T>
    static T static_add( T a, T b) {
        std::cout << "foo::static_add(" << a << "," << b << ") -> " << a+b << "\n";
        return a + b;
    }
};

int main() {
    Window window(sf::VideoMode(600, 600),"Test Window",7U,sf::ContextSettings());

    foo bar;
    auto bar_add = [&bar](auto a,auto b){ return bar.add<int>(a,b); };
    auto bind_add = std::bind(&foo::add<int>, &bar, std::placeholders::_1, std::placeholders::_2);

    window.addQueue(bind_add,5,2);
    window.addQueue(bar_add,5,2);
    window.addQueue(add<int>,5,2);
    window.addQueue(foo::static_add<int>,5,2);

    window.execQueue();
    return 0;
}

在这里尝试一样使用它 https://wwwww.onlinegdb.com/t9wczrl-czrl-czrl-c

The big problem with your code is that you take the function pointer as a pointer

template <typename F, typename ...Args>
void Enqueue(F (*f), Args... args)

and

template <typename F, typename ...Args>
void addQueue(F (*f), Args...args);

What you really want is

template <typename F, typename ...Args>
void Enqueue(F f, Args... args)

and

template <typename F, typename ...Args>
void addQueue(F f, Args...args);

And then you can pass any callable thing. Then you can use it with results of bind, lambdas, class member functions, class static member functions, and normal functions.

Then you can use it like

template<typename T>
int add(T a, T b) {
    std::cout << "add(" << a << "," << b << ") -> " << a+b << "\n";
    return a + b;
}

class foo{
public:
    template<typename T>
    T add( T a, T b) {
        std::cout << "foo::add(" << a << "," << b << ") -> " << a+b << "\n";
        return a + b;
    }

    template<typename T>
    static T static_add( T a, T b) {
        std::cout << "foo::static_add(" << a << "," << b << ") -> " << a+b << "\n";
        return a + b;
    }
};

int main() {
    Window window(sf::VideoMode(600, 600),"Test Window",7U,sf::ContextSettings());

    foo bar;
    auto bar_add = [&bar](auto a,auto b){ return bar.add<int>(a,b); };
    auto bind_add = std::bind(&foo::add<int>, &bar, std::placeholders::_1, std::placeholders::_2);

    window.addQueue(bind_add,5,2);
    window.addQueue(bar_add,5,2);
    window.addQueue(add<int>,5,2);
    window.addQueue(foo::static_add<int>,5,2);

    window.execQueue();
    return 0;
}

Try it here https://www.onlinegdb.com/t9WczrL-c

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文