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. Non-Templatized methods in Template Class?

Non-Templatized methods in Template Class?

Scheduled Pinned Locked Moved C / C++ / MFC
questionc++graphicslounge
7 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.
  • B Offline
    B Offline
    bob16972
    wrote on last edited by
    #1

    This is probably a dumb question, but I was examining the vector class source code (Visual C++ 2003) and noticed some method definitions that were confusing me. After some thought, I got on this tangent about template definitions in general and started to question why all the method definitions needed to be "templatized" (for lack of a better word) if some methods never dealt directly with any "typename" for the template. In other words, if a template method (lets call it "GetVersion") did something simple like return an integer constant, irregardless of any "typename", and the class was instantiated twice, lets say...

    CMyClass first;
    CMyClass second;

    and then somewhere in the code, each instance (first and second) calls "GetVersion", I'm assuming only one copy of "GetVersion" gets created by the compiler. If this is true, why does the method definition need to "templatized"? Or Does it need to be "templatized"? The code for vector almost appears to be taking advantage of some technique that only bothers with template syntax for the methods that deal with any typename but I could easily have been deceived. Is it possible to define non-templatized methods in a template class or am I just off my rocker? Or does the compiler require template syntax for every method? (So far, I only get compiler errors if I try to omit template syntax for such methods so I'm guessing I've strayed from reality a bit here but I thought I'd ask anyway)

    S C 3 Replies Last reply
    0
    • B bob16972

      This is probably a dumb question, but I was examining the vector class source code (Visual C++ 2003) and noticed some method definitions that were confusing me. After some thought, I got on this tangent about template definitions in general and started to question why all the method definitions needed to be "templatized" (for lack of a better word) if some methods never dealt directly with any "typename" for the template. In other words, if a template method (lets call it "GetVersion") did something simple like return an integer constant, irregardless of any "typename", and the class was instantiated twice, lets say...

      CMyClass first;
      CMyClass second;

      and then somewhere in the code, each instance (first and second) calls "GetVersion", I'm assuming only one copy of "GetVersion" gets created by the compiler. If this is true, why does the method definition need to "templatized"? Or Does it need to be "templatized"? The code for vector almost appears to be taking advantage of some technique that only bothers with template syntax for the methods that deal with any typename but I could easily have been deceived. Is it possible to define non-templatized methods in a template class or am I just off my rocker? Or does the compiler require template syntax for every method? (So far, I only get compiler errors if I try to omit template syntax for such methods so I'm guessing I've strayed from reality a bit here but I thought I'd ask anyway)

      S Offline
      S Offline
      Stefan_Lang
      wrote on last edited by
      #2

      This is a very good question. I do think that a function within a templated class, such as GetVersion() will indeed be implemented for each class the template gets instantiated for, creating redundand code. If this is not the case I'd really like to know from someone more knowledgable than me. You can however avoid it with various tricks, such as moving the function(s) in question out of the class definition, or into a base class that doesn't depend on a template class argument.

      1 Reply Last reply
      0
      • B bob16972

        This is probably a dumb question, but I was examining the vector class source code (Visual C++ 2003) and noticed some method definitions that were confusing me. After some thought, I got on this tangent about template definitions in general and started to question why all the method definitions needed to be "templatized" (for lack of a better word) if some methods never dealt directly with any "typename" for the template. In other words, if a template method (lets call it "GetVersion") did something simple like return an integer constant, irregardless of any "typename", and the class was instantiated twice, lets say...

        CMyClass first;
        CMyClass second;

        and then somewhere in the code, each instance (first and second) calls "GetVersion", I'm assuming only one copy of "GetVersion" gets created by the compiler. If this is true, why does the method definition need to "templatized"? Or Does it need to be "templatized"? The code for vector almost appears to be taking advantage of some technique that only bothers with template syntax for the methods that deal with any typename but I could easily have been deceived. Is it possible to define non-templatized methods in a template class or am I just off my rocker? Or does the compiler require template syntax for every method? (So far, I only get compiler errors if I try to omit template syntax for such methods so I'm guessing I've strayed from reality a bit here but I thought I'd ask anyway)

        S Offline
        S Offline
        Stefan_Lang
        wrote on last edited by
        #3

        I've thought about it a bit, and found something that forces the compiler to create individual methods, even if it considers the code of the entire project. What I am thinking of is Template specialization: any code using a template class may define a specialized implementation of an otherwise 'non-templatized' method, and that might happen even outside the project the compiler is looking at:

        // MyClass.h
        template
        class MyClass {
        public:
        int version() const { return 1; }
        };

        // someOtherFile_possiblyEvenWithinAnotherProject.h
        #include "MyClass.h"
        template <>
        class MyClass {
        public:
        int version() const { return 2; }
        };

        B 1 Reply Last reply
        0
        • B bob16972

          This is probably a dumb question, but I was examining the vector class source code (Visual C++ 2003) and noticed some method definitions that were confusing me. After some thought, I got on this tangent about template definitions in general and started to question why all the method definitions needed to be "templatized" (for lack of a better word) if some methods never dealt directly with any "typename" for the template. In other words, if a template method (lets call it "GetVersion") did something simple like return an integer constant, irregardless of any "typename", and the class was instantiated twice, lets say...

          CMyClass first;
          CMyClass second;

          and then somewhere in the code, each instance (first and second) calls "GetVersion", I'm assuming only one copy of "GetVersion" gets created by the compiler. If this is true, why does the method definition need to "templatized"? Or Does it need to be "templatized"? The code for vector almost appears to be taking advantage of some technique that only bothers with template syntax for the methods that deal with any typename but I could easily have been deceived. Is it possible to define non-templatized methods in a template class or am I just off my rocker? Or does the compiler require template syntax for every method? (So far, I only get compiler errors if I try to omit template syntax for such methods so I'm guessing I've strayed from reality a bit here but I thought I'd ask anyway)

          C Offline
          C Offline
          CPallini
          wrote on last edited by
          #4

          Of course the compiler will generate two 'GetVersion' methods, one for each class. Please note, they have even different signature. :)

          If the Lord God Almighty had consulted me before embarking upon the Creation, I would have recommended something simpler. -- Alfonso the Wise, 13th Century King of Castile.
          This is going on my arrogant assumptions. You may have a superb reason why I'm completely wrong. -- Iain Clarke
          [My articles]

          B 1 Reply Last reply
          0
          • C CPallini

            Of course the compiler will generate two 'GetVersion' methods, one for each class. Please note, they have even different signature. :)

            If the Lord God Almighty had consulted me before embarking upon the Creation, I would have recommended something simpler. -- Alfonso the Wise, 13th Century King of Castile.
            This is going on my arrogant assumptions. You may have a superb reason why I'm completely wrong. -- Iain Clarke
            [My articles]

            B Offline
            B Offline
            bob16972
            wrote on last edited by
            #5

            You're right. I don't know what I was thinking. I think my reality got altered a bit looking at the source code for vector and I started to believe the world was flat. :doh: :-O Thanks for helping straighten me back out! :-D

            1 Reply Last reply
            0
            • S Stefan_Lang

              I've thought about it a bit, and found something that forces the compiler to create individual methods, even if it considers the code of the entire project. What I am thinking of is Template specialization: any code using a template class may define a specialized implementation of an otherwise 'non-templatized' method, and that might happen even outside the project the compiler is looking at:

              // MyClass.h
              template
              class MyClass {
              public:
              int version() const { return 1; }
              };

              // someOtherFile_possiblyEvenWithinAnotherProject.h
              #include "MyClass.h"
              template <>
              class MyClass {
              public:
              int version() const { return 2; }
              };

              B Offline
              B Offline
              bob16972
              wrote on last edited by
              #6

              Yeah. I'm now realizing I really lost track of reality on this one. I think I got turned around a bit trying to think about it too much (thinking is such a dangerous pastime). C# has been rotting my brain lately so I'll blame it on that. ;)

              S 1 Reply Last reply
              0
              • B bob16972

                Yeah. I'm now realizing I really lost track of reality on this one. I think I got turned around a bit trying to think about it too much (thinking is such a dangerous pastime). C# has been rotting my brain lately so I'll blame it on that. ;)

                S Offline
                S Offline
                Stefan_Lang
                wrote on last edited by
                #7

                No worries. It was a good question. I had to think for quite a time to come up with a good reason. While all you could get out of preventing the redundand template instantiation would be a few bytes less in your executable, I appreciate that some people at least consider the possibility to avoid unneccessary clutter. If all software developers were like you, Windows would run on 500 KB rather than 5 GB. :)

                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