तो, मैं टेम्पलेट metaprogramming बेहतर जानने की कोशिश कर रहा हूँ और मैं समझ यह इसके लिए एक अच्छा व्यायाम है।
मैं कोड लिखने की कोशिश कर रहा हूं जो किसी भी प्रकार के तर्कों के साथ किसी फ़ंक्शन को कॉलबैक कर सकता है जिसे मैं पास करता हूं।
// First function to call int add(int x, int y) ; // Second function to call double square(double x) ; // Third func to call void go() ;
कॉलबैक निर्माण कोड दिखना चाहिए:
// Write a callback object that // will be executed after 42ms for "add" Callback<int, int, int> c1 ; c1.func = add ; c1.args.push_back(2); // these are the 2 args c1.args.push_back(5); // to pass to the "add" function // when it is called Callback<double, double> c2 ; c2.func = square ; c2.args.push_back(52.2) ;
मैं क्या सोच रहा हूँ है, टेम्पलेट metaprogramming का उपयोग कर रहा है, जैसे कॉलबैक की घोषणा इस तरह एक struct लिखने में सक्षम होना चाहता हूँ (कृपया ध्यान रखें यह बहुत स्यूडोकोड) जब
साथ कहा जाता है के लिए तो<TEMPLATING ACTION <<ANY NUMBER OF TYPES GO HERE>> > struct Callback { double execTime ; // when to execute TYPE1 (*func)(TYPE2 a, TYPE3 b) ; void* argList ; // a stored list of arguments // to plug in when it is time to call __func__ } ;
है
Callback<int, int, int> c1 ;
आप स्वचालित रूप से < हार्डकोर templating कार्रवाई >
struct Callback { double execTime ; // when to execute int (*func)(int a, int b) ; void* argList ; // this would still be void*, // but I somehow need to remember // the types of the args.. } ;
सही दिशा में किसी भी संकेत दिए गए की तरह एक struct द्वारा आप के लिए निर्माण किया जायेगा इस लिखने पर आरंभ करने के लिए?
वैराडिक टेम्पलेट्स सी ++ 0x का हिस्सा हैं। उनका समर्थन करने वाला एक कंपाइलर (या यहां तक कि कम C++ 0x समर्थन वाले कंपाइलर) में सी ++ 0x फ़ंक्शन ऑब्जेक्ट्स और 'std :: bind' भी शामिल होंगे, जो' boost :: bind' के समान है और जो भी हो के बारे में बातें कर रहे हैं। – Potatoswatter