c++ - Problems with Delegate Template Metaprogramming -
the below code adapted programmer yingle jia's code , i'm having port linux. compiles fine in vs2010, when try building in ubuntu gcc 4.6.3, shows errors @
template <class r acf_delegate_comma acf_delegate_template_params> class delegate<r (acf_delegate_template_args)>
the error message being:
../../../mysdk-master/include/abc/delegatetemplate.h:45:1: error: pasting "," , "class" not give valid preprocessing token ../../../mysdk-master/include/abc/delegatetemplate.h:46:1: error: pasting "," , "t" not give valid preprocessing token ../../../mysdk-master/include/abc/delegatetemplate.h:74:1: error: pasting "," , "a" not give valid preprocessing token
lines 45 , 46 2 lines of code of delegatetemplate.h
i've pasted above.
delegate.h
// copyright (c) 2004-2005 yingle jia // // permission copy, use, modify, sell , distribute software // granted provided copyright notice appears in copies. // software provided "as is" without express or implied warranty, // , no claim suitability purpose. // // acfdelegate.h // #ifndef __acf_delegate__ #define __acf_delegate__ #ifndef __acf_corlib__ #include <stdexcept> // std::runtime_error #endif // #ifndef __acf_corlib__ #include <utility> // std::pair // macros template metaprogramming #define acf_join(a, b) acf_do_join(a, b) #define acf_do_join(a, b) acf_do_join2(a, b) #define acf_do_join2(a, b) a##b #define acf_make_params1_0(t) #define acf_make_params1_1(t) t##1 #define acf_make_params1_2(t) t##1, ##t##2 #define acf_make_params1_3(t) t##1, ##t##2, ##t##3 #define acf_make_params1_4(t) t##1, ##t##2, ##t##3, ##t##4 #define acf_make_params1_5(t) t##1, ##t##2, ##t##3, ##t##4, ##t##5 #define acf_make_params1_6(t) t##1, ##t##2, ##t##3, ##t##4, ##t##5, ##t##6 #define acf_make_params2_0(t1, t2) #define acf_make_params2_1(t1, t2) t1##1 t2##1 #define acf_make_params2_2(t1, t2) t1##1 t2##1, t1##2 t2##2 #define acf_make_params2_3(t1, t2) t1##1 t2##1, t1##2 t2##2, t1##3 t2##3 #define acf_make_params2_4(t1, t2) t1##1 t2##1, t1##2 t2##2, t1##3 t2##3, t1##4 t2##4 #define acf_make_params2_5(t1, t2) t1##1 t2##1, t1##2 t2##2, t1##3 t2##3, t1##4 t2##4, t1##5 t2##5 #define acf_make_params2_6(t1, t2) t1##1 t2##1, t1##2 t2##2, t1##3 t2##3, t1##4 t2##4, t1##5 t2##5, t1##6 t2##6 #define acf_make_params1(n, t) acf_join(acf_make_params1_, n) (t) #define acf_make_params2(n, t1, t2) acf_join(acf_make_params2_, n) (t1, t2) namespace core { #ifndef __acf_corlib__ class invalidoperationexception : public std::runtime_error { public: invalidoperationexception() : std::runtime_error("invalidate operation") { } }; #endif // #ifndef __acf_corlib__ template <class tsignature> class delegate; // no body } // specializations #define acf_delegate_num_args 0 // delegate<r ()> #include "delegatetemplate.h" #undef acf_delegate_num_args #define acf_delegate_num_args 1 // delegate<r (t1)> #include "delegatetemplate.h" #undef acf_delegate_num_args #define acf_delegate_num_args 2 // delegate<r (t1, t2)> #include "delegatetemplate.h" #undef acf_delegate_num_args #define acf_delegate_num_args 3 // delegate<r (t1, t2, t3)> #include "delegatetemplate.h" #undef acf_delegate_num_args #define acf_delegate_num_args 4 // delegate<r (t1, t2, t3, t4)> #include "delegatetemplate.h" #undef acf_delegate_num_args #define acf_delegate_num_args 5 // delegate<r (t1, t2, t3, t4, t5)> #include "delegatetemplate.h" #undef acf_delegate_num_args #define acf_delegate_num_args 6 // delegate<r (t1, t2, t3, t4, t5, t6)> #include "delegatetemplate.h" #undef acf_delegate_num_args #define n_invoke(delegate,params) { if(delegate) delegate.invoke params; } #define n_event_handler(a) std::make_pair(this,&a); #endif // #ifndef __acf_delegate__
and small part of delegatetemplate.h
// copyright (c) 2004-2005 yingle jia // // permission copy, use, modify, sell , distribute software // granted provided copyright notice appears in copies. // software provided "as is" without express or implied warranty, // , no claim suitability purpose. // // acfdelegatetemplate.h // // note: header header template , must not have multiple-inclusion // protection. #define acf_delegate_template_params acf_make_params1(acf_delegate_num_args, class t) // class t0, class t1, class t2, ... #define acf_delegate_template_args acf_make_params1(acf_delegate_num_args, t) // t0, t1, t2, ... #define acf_delegate_function_params acf_make_params2(acf_delegate_num_args, t, a) // t0 a0, t1 a1, t2 a2, ... #define acf_delegate_function_args acf_make_params1(acf_delegate_num_args, a) // a0, a1, a2, ... //// comma if nonzero number of arguments #if acf_delegate_num_args == 0 #define acf_delegate_comma #else #define acf_delegate_comma , #endif namespace core { //------------------------------------------------------------------------- // class delegate<r (t1, t2, ..., tn)> template <class r acf_delegate_comma acf_delegate_template_params> class delegate<r (acf_delegate_template_args)> { // declarations private: class delegateimplbase { // fields public: delegateimplbase* previous; // singly-linked list // constructor/destructor protected: delegateimplbase() : previous(null) { } delegateimplbase(const delegateimplbase& other) : previous(null) { } public: virtual ~delegateimplbase() { } // methods public: virtual delegateimplbase* clone() const = 0; virtual r invoke(acf_delegate_function_params) const = 0; };
is because of lack of template metaprogramming support in gcc? checked whether because of variadic templates, that's supposed fixed in gcc 4.3 itself. grateful if solving error.
solved. went through this page , understood how token pasting operator supposed used:
turns out there ##
operators visual studio ignored, gcc didn't. lines changed these:
#define acf_make_params1_0(t) #define acf_make_params1_1(t) t ## 1 #define acf_make_params1_2(t) t ## 1, t ## 2 #define acf_make_params1_3(t) t ## 1, t ## 2, t ## 3 #define acf_make_params1_4(t) t ## 1, t ## 2, t ## 3, t ## 4 #define acf_make_params1_5(t) t ## 1, t ## 2, t ## 3, t ## 4, t ## 5 #define acf_make_params1_6(t) t ## 1, t ## 2, t ## 3, t ## 4, t ## 5, t ## 6
the lines earlier had ##t##2
creating error. changed t##2
, worked! glory hallelujah! :-) ends ordeal template metaprogramming.
Comments
Post a Comment