templated classes

This is a discussion on templated classes within the C++ Programming forums, part of the General Programming Boards category; When defining a function for a templated class, is the part in bold necessary in the function header? Code: template ...

  1. #1
    C/C++ homeyg's Avatar
    Join Date
    Nov 2004
    Location
    Louisiana, USA
    Posts
    209

    templated classes

    When defining a function for a templated class, is the part in bold necessary in the function header?

    Code:
    template <class mytype> void myclass<mytype>::myfunct(int, int)
    {
         //blah
    }

  2. #2
    Devil's Advocate SlyMaelstrom's Avatar
    Join Date
    May 2004
    Location
    Out of scope
    Posts
    4,062
    Yes, I believe it is.

    Why, because at that point, there is no such thing as myclass without a template, so just qualifying with myclass:: would be like qualifying with a class you never defined in the first place.
    Sent from my iPadŽ

  3. #3
    Registered User
    Join Date
    Dec 2004
    Location
    UK
    Posts
    109
    If I rember correctly the entire definition of a template class has to be within a single header file (I think it has to do with the way the compiler instatiaties templates), thus it's quite unusual for the definition of member function to be outside the main class definition.

    I do believe that if you brought it outside you would have to use that sintax.

    And I could be wrong about the all in 1 header part, but I've nver seen template classes split between .h and .cpp files

  4. #4
    Registered User
    Join Date
    Feb 2006
    Posts
    312
    The other reason, is to do with "overloading" of templated types (also known as specialisation). You may have certain special cases where the generic solution that myclass provides is insufficient, and need to provide a mechanism for users of myclass to take care of those special cases, and at the same time, provide the same interface.
    Hence, myclass:: alone could be ambiguous to the compiler. for example, if you had a partial specialisation which dealt with pointers:
    Code:
    template <mytype> void myclass<mytype*>::myfunct(int, int)
    {
         //blah
    };
    or, maybe another class requires special attention,
    Code:
    template <> void myclass<SomeOtherClass>::myfunct(int, int)
    {
         //blah
    };
    The behaviour of the compiler is such that any call by the user of myclass will try to match up with specialisations before resorting to the generic version.

    Code:
    //end waffle.

  5. #5
    Registered User
    Join Date
    Apr 2003
    Posts
    2,662
    Yes. You have to qualify the function name with its class name, but there is no such thing as a class called "myclass". There will only be classes named "myclass<int>" or "myclass<double>", etc.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. storing handlers (templated classes)
    By l2u in forum C++ Programming
    Replies: 3
    Last Post: 05-06-2008, 11:16 AM
  2. templated members in non-template classes
    By drrngrvy in forum C++ Programming
    Replies: 3
    Last Post: 04-07-2007, 01:38 PM
  3. Templated classes...
    By Wraithan in forum C++ Programming
    Replies: 7
    Last Post: 09-14-2006, 04:31 PM
  4. Templated singleton classes
    By VirtualAce in forum C++ Programming
    Replies: 2
    Last Post: 07-01-2006, 04:06 AM
  5. Pointers to (templated) classes being declared
    By reanimated in forum C++ Programming
    Replies: 2
    Last Post: 01-02-2006, 08:30 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21