Funktionen in einer Liste oder einem Array
-
Hallo,
Ich bin relativ neu im programmieren mit c++ und wollte fragen ob es irgendwie möglich ist funktionen in einer liste oder einem array zu speichern.
Ich möchte gerne einen threadpool schreiben bei dem man neue funktionen über eine methode addFunction zu einer liste oder einem array hinzufügt.
Ist das überhaupt möglich und wenn ja welchen typ muss dann die liste haben?
Wie würde man soetwas sonst programmieren?
Etwas ähnliches habe ich in python schonmal geschrieben, aber in pythons listen kann man alle typen gemischt halten.MfG init-0
-
Ja, das ist möglich. Schau dir dazu folgende Konzepte an: STL-Container, Funktionszeiger, Funktionsobjekte (speziell
std::function).
-
Das Stichwort ist function pointer.
-
...
-
Ich wollte einfach nur einen Anreiz zum googeln geben. ^^
Natürlich ist std::function besser.
-
Tut mir leid, dass es so lange gedauert hat hier zu antworten.
Ich habe jetzt versucht das ganze zusammen zu schreiben.
http://en.cppreference.com/w/cpp/utility/functional/function habe ich als Quelle für die lambdas benutzt.
Was ich eigentlich haben will ist so etwas:class ThreadPool{ FUNCLIST queue; void addFunction(std::function<void> func){ this->queue.insert(this->queue.end(), func); } }Aber zuerst habe ich versucht die queue im konstruktor zu benutzen:
#include <list> #include <functional> typedef std::list<std::function<void>> FUNCLIST; void print_num(int i) { printf("%d", i); } class ThreadPool{ FUNCLIST queue; ThreadPool(){ std::function<void> f_display_42 = []() { print_num(42); }; this->queue.insert(this->queue.end(), f_display_42); } }Leider verstehe ich die Fehlermeldung nicht so ganz:
1>------ Build started: Project: GET, Configuration: Debug Win32 ------ 1> GET.cpp 1>c:\program files (x86)\microsoft visual studio 11.0\vc\include\functional(554): error C2027: use of undefined type 'std::_Get_function_impl<_Tx>' 1> with 1> [ 1> _Tx=void 1> ] 1> c:\users\robin\documents\visual studio 2012\projects\get\get\threading.h(16) : see reference to class template instantiation 'std::function<_Fty>' being compiled 1> with 1> [ 1> _Fty=void 1> ] 1>c:\program files (x86)\microsoft visual studio 11.0\vc\include\functional(555): error C2504: 'type' : base class undefined 1>c:\program files (x86)\microsoft visual studio 11.0\vc\include\functional(558): error C2027: use of undefined type 'std::_Get_function_impl<_Tx>' 1> with 1> [ 1> _Tx=void 1> ] 1>c:\program files (x86)\microsoft visual studio 11.0\vc\include\functional(558): error C2146: syntax error : missing ';' before identifier '_Mybase' 1>c:\program files (x86)\microsoft visual studio 11.0\vc\include\functional(558): error C4430: missing type specifier - int assumed. Note: C++ does not support default-int ========== Build: 0 succeeded, 1 failed, 0 up-to-date, 0 skipped ==========
-
Aus Deinem Link:
template< class R, class... Args > class function<R(Args...)>Eine Funktion ohne Parameter, die nix zurückgibt:
std::function<void()> f = [](){};