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: how to detect type

Template: how to detect type

Scheduled Pinned Locked Moved C / C++ / MFC
tutorialquestion
8 Posts 7 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.
  • 4 Offline
    4 Offline
    4288
    wrote on last edited by
    #1

    I wrote a template class which can accept various data types (I had different kind of data to handle) All the data type I used with this template-class, are structures with common parameters. Now I need to write a common code to all classes (I was thinking of a function with his own class) to use these "common" parameters. The solutions I thought are: 1) Passing a void* pointer to the function and select the appropriate code for the datatype: void Function( void *pointer_to_custom_data, ENUM_DATA_TYPE) { if(ENUM_DATA_TYPE == DATA_TYPE1) // specific code for type1.. else if (ENUM_DATA_TYPE == DATA_TYPE2) // specific code for type2.. ... } 2) Overloading in some way this function (don't know how though) so it recognize the type used and uses the specific code Any better idea or suggestion on how to implement these?

    ---

    C L S _ S 6 Replies Last reply
    0
    • 4 4288

      I wrote a template class which can accept various data types (I had different kind of data to handle) All the data type I used with this template-class, are structures with common parameters. Now I need to write a common code to all classes (I was thinking of a function with his own class) to use these "common" parameters. The solutions I thought are: 1) Passing a void* pointer to the function and select the appropriate code for the datatype: void Function( void *pointer_to_custom_data, ENUM_DATA_TYPE) { if(ENUM_DATA_TYPE == DATA_TYPE1) // specific code for type1.. else if (ENUM_DATA_TYPE == DATA_TYPE2) // specific code for type2.. ... } 2) Overloading in some way this function (don't know how though) so it recognize the type used and uses the specific code Any better idea or suggestion on how to implement these?

      ---

      C Offline
      C Offline
      Code o mat
      wrote on last edited by
      #2

      I'm not sure i completely understand your problem, but the first thought that comes to my mind is: why don't you have a common base class for your structures that contains the common "parameters" you are talking about, then you can make a method that takes a pointer/reference to this base class and does with the parameters what it should. Or do you mean that you have to do different things depending on the type of struct? Then use a virtual function declared in the base class and implement it according to your needs in all the subclasses. Am i misunderstanding you?

      > The problem with computers is that they do what you tell them to do and not what you want them to do. < > Sometimes you just have to hate coding to do it well. <

      1 Reply Last reply
      0
      • 4 4288

        I wrote a template class which can accept various data types (I had different kind of data to handle) All the data type I used with this template-class, are structures with common parameters. Now I need to write a common code to all classes (I was thinking of a function with his own class) to use these "common" parameters. The solutions I thought are: 1) Passing a void* pointer to the function and select the appropriate code for the datatype: void Function( void *pointer_to_custom_data, ENUM_DATA_TYPE) { if(ENUM_DATA_TYPE == DATA_TYPE1) // specific code for type1.. else if (ENUM_DATA_TYPE == DATA_TYPE2) // specific code for type2.. ... } 2) Overloading in some way this function (don't know how though) so it recognize the type used and uses the specific code Any better idea or suggestion on how to implement these?

        ---

        L Offline
        L Offline
        Lost User
        wrote on last edited by
        #3

        You should, if possible, make the function part of the template class, thus ensuring type safety throughout. See the MSDN documentation on Templates[^] for more information.

        It's time for a new signature.

        1 Reply Last reply
        0
        • 4 4288

          I wrote a template class which can accept various data types (I had different kind of data to handle) All the data type I used with this template-class, are structures with common parameters. Now I need to write a common code to all classes (I was thinking of a function with his own class) to use these "common" parameters. The solutions I thought are: 1) Passing a void* pointer to the function and select the appropriate code for the datatype: void Function( void *pointer_to_custom_data, ENUM_DATA_TYPE) { if(ENUM_DATA_TYPE == DATA_TYPE1) // specific code for type1.. else if (ENUM_DATA_TYPE == DATA_TYPE2) // specific code for type2.. ... } 2) Overloading in some way this function (don't know how though) so it recognize the type used and uses the specific code Any better idea or suggestion on how to implement these?

          ---

          S Offline
          S Offline
          Saurabh Garg
          wrote on last edited by
          #4

          This looks likes bad design to me. Template class should not be explicitly aware of the types it can be used with. It can define the requirements in the types but that is all. In your case it looks like better option is to use inheritance instead of template class. Unless of course you have presented a simplified picture. I will suggest you inherit the structures from a common base class. This base class will contain common parameters and a pure virtual function. Inherited structures will override this function providing the type specific functionality. -Saurabh

          1 Reply Last reply
          0
          • 4 4288

            I wrote a template class which can accept various data types (I had different kind of data to handle) All the data type I used with this template-class, are structures with common parameters. Now I need to write a common code to all classes (I was thinking of a function with his own class) to use these "common" parameters. The solutions I thought are: 1) Passing a void* pointer to the function and select the appropriate code for the datatype: void Function( void *pointer_to_custom_data, ENUM_DATA_TYPE) { if(ENUM_DATA_TYPE == DATA_TYPE1) // specific code for type1.. else if (ENUM_DATA_TYPE == DATA_TYPE2) // specific code for type2.. ... } 2) Overloading in some way this function (don't know how though) so it recognize the type used and uses the specific code Any better idea or suggestion on how to implement these?

            ---

            _ Offline
            _ Offline
            _Superman_
            wrote on last edited by
            #5

            If you want type specific code, you do not write a template class. In some cases, for template classes you need a special implementation for a certain type. In these cases you can do template specialization. Assume you have a template class called Math -

            template
            class Math
            {
            };

            You may need special implementation for some data type like complex. In this case you can specialize the template class for the complex data type.

            template<>
            class Math
            {
            };

            «_Superman_» I love work. It gives me something to do between weekends.
            Microsoft MVP (Visual C++)

            1 Reply Last reply
            0
            • 4 4288

              I wrote a template class which can accept various data types (I had different kind of data to handle) All the data type I used with this template-class, are structures with common parameters. Now I need to write a common code to all classes (I was thinking of a function with his own class) to use these "common" parameters. The solutions I thought are: 1) Passing a void* pointer to the function and select the appropriate code for the datatype: void Function( void *pointer_to_custom_data, ENUM_DATA_TYPE) { if(ENUM_DATA_TYPE == DATA_TYPE1) // specific code for type1.. else if (ENUM_DATA_TYPE == DATA_TYPE2) // specific code for type2.. ... } 2) Overloading in some way this function (don't know how though) so it recognize the type used and uses the specific code Any better idea or suggestion on how to implement these?

              ---

              S Offline
              S Offline
              Stuart Dootson
              wrote on last edited by
              #6

              Function specialisation. Specialization if you're American. Use the standard template case for the common code, specialisations for code specific to types. Like this:

              #include <iostream>

              template<class _Type>
              void Fn(_Type const&)
              {
              std::cout << "Generic..." << std::endl;
              }

              template<>
              void Fn<int>(int const&)
              {
              std::cout << "int specialisation" << std::endl;
              }

              int main()
              {
              Fn('a');
              Fn(10);
              }

              The output you get is:

              Generic...
              int specialisation

              Java, Basic, who cares - it's all a bunch of tree-hugging hippy cr*p CodeProject MVP for 2010 - who'd'a thunk it!

              4 1 Reply Last reply
              0
              • S Stuart Dootson

                Function specialisation. Specialization if you're American. Use the standard template case for the common code, specialisations for code specific to types. Like this:

                #include <iostream>

                template<class _Type>
                void Fn(_Type const&)
                {
                std::cout << "Generic..." << std::endl;
                }

                template<>
                void Fn<int>(int const&)
                {
                std::cout << "int specialisation" << std::endl;
                }

                int main()
                {
                Fn('a');
                Fn(10);
                }

                The output you get is:

                Generic...
                int specialisation

                Java, Basic, who cares - it's all a bunch of tree-hugging hippy cr*p CodeProject MVP for 2010 - who'd'a thunk it!

                4 Offline
                4 Offline
                4288
                wrote on last edited by
                #7

                I'm not dead, I was just looking at your suggestions and see whether they're appliable to my project. Thank you guys but I realized some things: 1) I'm an idiot 2) More than half of my project has to be rewritten. I can't specializate template class because datatype are too much. 3) Project is slow as hell, I screwed it up with template of templates and at runtime it has to calculate every damn address it uses I think I'm going to change a lot of things... thank you for your help!

                ---

                1 Reply Last reply
                0
                • 4 4288

                  I wrote a template class which can accept various data types (I had different kind of data to handle) All the data type I used with this template-class, are structures with common parameters. Now I need to write a common code to all classes (I was thinking of a function with his own class) to use these "common" parameters. The solutions I thought are: 1) Passing a void* pointer to the function and select the appropriate code for the datatype: void Function( void *pointer_to_custom_data, ENUM_DATA_TYPE) { if(ENUM_DATA_TYPE == DATA_TYPE1) // specific code for type1.. else if (ENUM_DATA_TYPE == DATA_TYPE2) // specific code for type2.. ... } 2) Overloading in some way this function (don't know how though) so it recognize the type used and uses the specific code Any better idea or suggestion on how to implement these?

                  ---

                  M Offline
                  M Offline
                  Moak
                  wrote on last edited by
                  #8

                  Hi 4288! My spontaneous reaction was, why does he use templates and not polymorphism... but I wanted to see what others say. Perhaps it can help you in your design to have an abstract base class CObject and then derive classes from that base class with further specialisation. You would then only need one template for the base class (or if it is about storing data something like std::vector<CObject*>). In C++ virtual methods will be called virtual even via a base class pointer, quite handy. Hard to give better tips without seeing your architecture or requirements. Looks like Saurabh gave the same advice. Hope it helps... and happy refactoring! :) /M

                  Webchat in Europe :java: Now with 26% more Twitter

                  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