Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C / C++ / MFC
  4. Template argument dependent compilation

Template argument dependent compilation

Scheduled Pinned Locked Moved C / C++ / MFC
helptutoriallearning
4 Posts 3 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • T Offline
    T Offline
    tuxyboy
    wrote on last edited by
    #1

    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....

    C V 2 Replies Last reply
    0
    • T tuxyboy

      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....

      C Offline
      C Offline
      Chris Losinger
      wrote on last edited by
      #2

      i believe the term you need to look into is "template specialization"

      image processing | blogging

      1 Reply Last reply
      0
      • T tuxyboy

        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....

        V Offline
        V Offline
        Viorel
        wrote on last edited by
        #3

        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.

        T 1 Reply Last reply
        0
        • V Viorel

          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.

          T Offline
          T Offline
          tuxyboy
          wrote on last edited by
          #4

          I already figured but thanks, anyway. Anybody dealing with the same issue should know to have all the specialized functions inline in the header file if want to get rid of LNK2006 errors..

          1 Reply Last reply
          0
          Reply
          • Reply as topic
          Log in to reply
          • Oldest to Newest
          • Newest to Oldest
          • Most Votes


          • Login

          • Don't have an account? Register

          • Login or register to search.
          • First post
            Last post
          0
          • Categories
          • Recent
          • Tags
          • Popular
          • World
          • Users
          • Groups