Bonjour,
j'aimerais stocker des pointeurs sur des fonctions dans un std::vector pour pouvoir les appeler le moment venu.
Le problème c'est que c'est une fonction template et je ne vois pas trop comment déclarer mon vector.
voila un bout de code pour être plus clair:
Dans mon exemple j'appelle directement la fonction "equal", je voudrais la stocker dans "condition_list".
Code : Sélectionner tout - Visualiser dans une fenêtre à part
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
44
45
46
47
48
49
50 #include <string> #include <iostream> #include <boost/any.hpp> #include <map> #include <vector> using namespace std; class Toto { public: Toto() { args["arg1"] = 7; args["arg2"] = string("hello"); }; template <typename T> bool equal(string nom, T val) { if (typeid(val) == args[nom].type()) { return boost::any_cast<T>(args[nom]) == val; } else { cout << "deux types differents\n"; cout << "argument type: " << (args[nom].type()).name() << endl; cout << "condition type: " << typeid(val).name() << endl; return false; } } private: std::map<std::string, boost::any> args; // std::vector<> condition_list; }; int main(int argc, char* argv[]) { Toto t; cout << true << endl; cout << false << endl; cout << t.equal("arg1", 7) << endl; cout << t.equal("arg1", 3) << endl; cout << t.equal("arg2", string("hello")) << endl; cout << t.equal("arg2", string("gni!")) << endl; cout << t.equal("arg1", string("bah!")) << endl; return 0; }
Comme j'utilise déjà BOOST j'ai essayé de voir avec boost::function ou boost::any, mais je ne vois toujours pas...
Vous auriez une idée pour trouver un moyen de faire ça?
Partager