提问者:小点点

带模板参数函数的C++向量


#include<iostream>
#include<functional>
#include <unordered_set>
#include <vector>
#include <unordered_map>
#include <variant>




class Button
{
public:
    Button(int _val):val(_val){}
    ~Button();
    int val;
    
    template<typename F, typename...InArgs>
    void addListener(std::function<F(InArgs...)>(f), InArgs... args) {
        listener.push_back(f(args...));
    }

    template<typename F, typename...InArgs>
    void callMethod(std::function<F(InArgs...)>(f), InArgs... args) {
        f(args...);
    }
    
    void print() { std::cout << "Button value -> " << val << std::endl; }
private:
    template<typename F,typename...InArgs>
    static std::vector<std::function<F(InArgs...)>> listener;
    
};

inline void test1() {
    std::cout << "Printing test1 method...\n";
}
inline void test2(Button* b) {
    std::cout << "Button value is 20.\n";
    b->val = 20;
}
inline void test3(Button* b, int a) {
    std::cout << "Button value is passed parameter.\n";
    b->val = a;
}
inline void test4(int a) {
    std::cout << "print " << a << std::endl;
}
class AppThree
{
public:
    static void run() {
        Button* button = new Button(5);
        button->print();
        //button->callMethod(std::function<void(int)>(&test4), 1); //work it
        button->addListener(std::function<void(int)>(&test4),1); // error
        

    }
};

我想要推函数与参数向量。 我想从向量调用所有的函数,但我不能添加函数。 我收到C3245错误。 我不知道哪里失败了。 如果我的想法不正确,请显示正确的方式。 对不起,我的英语不好


共1个答案

匿名用户

您有一个成员变量模板侦听器,这意味着您对每个函数类型都有一个向量。 编译器无法推断您指的是哪个参数。
您还试图push_back调用函数的结果,而不是函数本身,并且您没有存储参数。

您不能按计划的方式执行此操作,因为listener不是一个具有不同类型元素的变量--它(可能)是许多变量,每个变量具有不同的类型。
为了调用它们,您需要枚举变量实例,但这是不可能的。
此外,您还需要找到存储参数的某种方法。

由于您希望稍后传递参数,因此只需要一种类型,您可以在lambda中捕获它们:

class Button
{
public:
    Button(int _val):val(_val){}
    ~Button();
    int val;
    
    template<typename F, typename...InArgs>
    void addListener(std::function<F(InArgs...)> f, InArgs... args) {
        listener.push_back([=]() { f(args...); });
    }
    void action()
    {
        for (auto& f: listener)
            f();
    }
    void print() { std::cout << "Button value -> " << val << std::endl; }
    
private:
    static std::vector<std::function<void()>> listener;
    
};

std::vector<std::function<void()>> Button::listener;

void test3(Button* b) {
    
    std::cout << "listened:";
    b->print();
}

void test4(int a) {
    
    std::cout << "print " << a << std::endl;
}

int main()
{
    Button* button = new Button(5);
    button->print();
    button->addListener(std::function<decltype(test4)>(&test4), 1);
    button->addListener(std::function<decltype(test3)>(&test3), button);
    button->action();
}

您也可以将监听器构造完全留给用户:

class Button
{
public:
    Button(int _val):val(_val){}
    ~Button();
    int val;
    
    void addListener(std::function<void()> f) {
        listener.push_back(f);
    }
    void action()
    {
        for (auto& f: listener)
            f();
    }
    void print() { std::cout << "Button value -> " << val << std::endl; }
    
private:
    static std::vector<std::function<void()>> listener;
    
};

std::vector<std::function<void()>> Button::listener;

void test3(Button* b) {
    
    std::cout << "listened:";
    b->print();
}

void test4(int a) {
    
    std::cout << "print " << a << std::endl;
}

int main()
{
    Button* button = new Button(5);
    button->print();
    button->addListener([]() { test4(1); });
    button->addListener([button]() { test3(button); });
    button->action();
}

相关问题


MySQL Query : SELECT * FROM v9_ask_question WHERE 1=1 AND question regexp '(模板|参数|函数|c++|向量)' ORDER BY qid DESC LIMIT 20
MySQL Error : Got error 'repetition-operator operand invalid' from regexp
MySQL Errno : 1139
Message : Got error 'repetition-operator operand invalid' from regexp
Need Help?