Template argument dependent compilation
-
I'm putting together code for a template that implements some special tasks that I need and I want it to use with different CWnd based classes as template argument and ancestor at the same time like this: template < class BASE > class CLASS : public BASE { . . . }; If I want to use it with CFormView and CDialog in the same project I need template argument dependent compiling, 'cause for example if I need to implement OnInitDialog() for CDialog, I must ignore it with CFormView, otherwise I get an error that this function is not the member of the base class. I thought of doing string comparison of the BASE argument at the right places, but I'm not too familiar with macros. #define STR_COMP(_base, _class) \ (strcmp(#_base, _class)==0) and use it as #if STR_COMP(BASE, "CDialog") ..... #else if ... .... #else ... #endif Of course it doesn't work....
-
I'm putting together code for a template that implements some special tasks that I need and I want it to use with different CWnd based classes as template argument and ancestor at the same time like this: template < class BASE > class CLASS : public BASE { . . . }; If I want to use it with CFormView and CDialog in the same project I need template argument dependent compiling, 'cause for example if I need to implement OnInitDialog() for CDialog, I must ignore it with CFormView, otherwise I get an error that this function is not the member of the base class. I thought of doing string comparison of the BASE argument at the right places, but I'm not too familiar with macros. #define STR_COMP(_base, _class) \ (strcmp(#_base, _class)==0) and use it as #if STR_COMP(BASE, "CDialog") ..... #else if ... .... #else ... #endif Of course it doesn't work....
i believe the term you need to look into is "template specialization"
-
I'm putting together code for a template that implements some special tasks that I need and I want it to use with different CWnd based classes as template argument and ancestor at the same time like this: template < class BASE > class CLASS : public BASE { . . . }; If I want to use it with CFormView and CDialog in the same project I need template argument dependent compiling, 'cause for example if I need to implement OnInitDialog() for CDialog, I must ignore it with CFormView, otherwise I get an error that this function is not the member of the base class. I thought of doing string comparison of the BASE argument at the right places, but I'm not too familiar with macros. #define STR_COMP(_base, _class) \ (strcmp(#_base, _class)==0) and use it as #if STR_COMP(BASE, "CDialog") ..... #else if ... .... #else ... #endif Of course it doesn't work....
Maybe you should try a "specialization" of template classes? I suppose it may look like this:
template< class BASE > class COMMON : public BASE { // common members for CDialog and CFormView base . . . }; template< class BASE > class CLASS : public COMMON< BASE > { // nothing yet here }; // specialization for CDialog: template< > class CLASS< CDialog > : public COMMON< CDialog > { // members specific to CDialog . . . }; // specialization for CFormView: template< > class CLASS< CFormView > : public COMMON< CFormView > { // members specific to CFormView . . . };
Usage:
CLASS< CDialog > dlg; CLASS< CFormView > form;
I hope this works.
-
Maybe you should try a "specialization" of template classes? I suppose it may look like this:
template< class BASE > class COMMON : public BASE { // common members for CDialog and CFormView base . . . }; template< class BASE > class CLASS : public COMMON< BASE > { // nothing yet here }; // specialization for CDialog: template< > class CLASS< CDialog > : public COMMON< CDialog > { // members specific to CDialog . . . }; // specialization for CFormView: template< > class CLASS< CFormView > : public COMMON< CFormView > { // members specific to CFormView . . . };
Usage:
CLASS< CDialog > dlg; CLASS< CFormView > form;
I hope this works.