CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 9 of 9
  1. #1
    Join Date
    Oct 2001
    Location
    Belgium
    Posts
    4

    Template overloading

    Hi there,

    I'm using template overloading but can anyone tell me the difference bewteen normal function overloading and template overloading.

    Template function:

    template<class T>
    T getParamater( int iWere, const T& iDefault )
    {
    // do something
    }

    overloading 1:
    double getParamater( int iWere, const double& iDefault )
    {
    // do something else
    }

    overloading 2:
    template<> double getParamater( int iWere, const double& iDefault )
    {
    // do something else
    }

    Is there a difference between overloading 1 and 2

    Greetings Tim

  2. #2
    Join Date
    Jun 2002
    Location
    Germany
    Posts
    1,557

    Overloaded!

    Tim,

    There are several interesting aspects of the supplied code example. Generally, providing a template is useful for repeating identical code sequences on several different data types. Within this context, there are sometimes slight differences in the desired template instance for a particular data type which merit the implementation of a template specialization for the particular data type. The example "overloading 2" is a a template specialization for the specialized type double.

    However the "overloading 1" is a pure overload. Providing this pure overload is allowed. However, some developers might seriously ask why it is needed in a clear design. It seems like it would usually be unnecessary since the template specialization should be able to handle the functional needs for double.

    An additional note: A good C++ compiler should be able to deduce the template parameter if none is explicitly provided. The implementation of "overloading 1" would actually mask the template instantiation in case one wanted to use the compiler's ability to deduce the template parameter.

    For a good design, I would recommend providing the template and using template specializations where needed. It is, in my opinion, not proper to implement overloading 1 in addition to the template specialization. Also, if your design calls for many specializations, then the the use of a template might not be well suited for the needed functionality. You need to examine the design goals.

    Chris.

    You're gonna go blind staring into that box all day.

  3. #3
    Join Date
    Mar 2002
    Location
    St. Petersburg, Florida, USA
    Posts
    12,125
    Dud is correct.

    There are however some conditions where it makes sence to do an explicit overload for CLASS templates.

    First there are those conditions where most of the code is deduceable by the compiler, but one or more functions needs manual intervention. This is usually better supported by making the member pure virtual in the template and deriving specific classes for each case. If only some of the instanciations of the template need manual intervention, or the method requires EXTREME speed, the manual overload is ideal.

    Second there is the ability to protect a template so that it can only be instanciated for certain template parameters. Simply declare but do not define a protected/private member function that takes the class type as the parameter. Manually implement this method for allowed classes. Now at link time, if a programmer goes to use the template on a class other than one of the "approved" classes, a link error will occur.


    ** There are few truely RIGHT answers that apply to all circumstances **
    TheCPUWizard is a registered trademark, all rights reserved. (If this post was helpful, please RATE it!)
    2008, 2009,2010
    In theory, there is no difference between theory and practice; in practice there is.

    * Join the fight, refuse to respond to posts that contain code outside of [code] ... [/code] tags. See here for instructions
    * How NOT to post a question here
    * Of course you read this carefully before you posted
    * Need homework help? Read this first

  4. #4
    Join Date
    Sep 2002
    Posts
    1,747

    one more point

    Also, if a function template identifier is also used for a family of
    overloaded functions, the search for which function to call
    takes these steps (in the given order):
    1. exact match from among the overloaded functions
    2. exact match from template instantiation
    3. best match from among the overloaded functions (ie. allowing type conversion)
    */*/*/*/*/*/*/*/*/*/*/*/*/*/*/*/*/*/

    "It's hard to believe in something you don't understand." -- the sidhi X-files episode

    galathaea: prankster, fablist, magician, liar

  5. #5
    Join Date
    Oct 2001
    Location
    Belgium
    Posts
    4
    Hi,
    Already thanks for the information.
    My template member function is for most of my types, but 3 class types expect another implementation so temlate overloading is justified I think. I know that there a some rules about choosing the functions depending on the type maching (if you use template oveloading there will never be a cast). I think type matching can make a difference if you use more then one template argument. I'm am using only one argument so it will never cast to another types because the template function is used instead.
    In this example is there any difference between the overload functions? Or is it just taking the one I like the most?

    Originally posted by TheCPUWizard
    This is usually better supported by making the member pure virtual in the template and deriving specific classes for each case.
    I'm not using template classes just a template member function in a normal class. Can I make this template member function pure virtual in the base class and implement it in the diriving classes?
    (MSVC compiler) If it can, how?

    Second there is the ability to protect a template so that it can only be instanciated for certain template parameters. Simply declare but do not define a protected/private member function that takes the class type as the parameter. Manually implement this method for allowed classes. Now at link time, if a programmer goes to use the template on a class other than one of the "approved" classes, a link error will occur.
    Thanks, can become very usefull , never ever had thought about that myself.

    Another question: Is there a way I can declare my templates in the headerfile and implement them in the cpp file (MSVC compiler)?

    Greetings Tim

  6. #6
    Join Date
    Oct 2001
    Location
    Dublin, Eire
    Posts
    880
    No. As far as I know, the implementation of a template must be in a public file (header).
    Elrond
    A chess genius is a human being who focuses vast, little-understood mental gifts and labors on an ultimately trivial human enterprise.
    -- George Steiner

  7. #7
    Join Date
    May 2000
    Location
    Phoenix, AZ [USA]
    Posts
    1,347
    I want to say that I agree with Elrond, but you COULD put the
    template definition in a .cpp file ... but you'd still have to #include
    that file. Remember, no object code can be instantiated for a
    template definition because the compiler still wouldn't know what
    type to create ... so the compiler waits until you actually
    instantiate a template before the proper functions are generated.
    That's why everything Elrond said is true.

    --Paul

  8. #8
    Join Date
    Jun 2002
    Location
    Germany
    Posts
    1,557

    Template mania

    Gurus,

    Thanks for all the rich information on the topic of templates. I just love 'em.

    Elrond, Paul: I think that there is a language keyword "export" which is intended to support template bodies within non-header source files.

    Evidently it is very challenging to fully support this keyword and very few compilers support it. I have never used this keyword except in so far as to verify that it is not (yet) supported by GCC 3.2 or Microsoft VC6/7 compilers.

    The Comeau compiler internet pages (http://www.comeaucomputing.com/) discuss this topic.

    Chris.

    You're gonna go blind staring into that box all day.

  9. #9
    Join Date
    Mar 2002
    Location
    St. Petersburg, Florida, USA
    Posts
    12,125
    A "possible" method to reduce the coupling between a template based class and the rest of the code is to implement as much work as possible in a non-template base class [all type invariant operations and members]. Then you derive the template from the base class.

    While this still leaves the template member functions "exposed" [the compiler does need to know what to write!], it can protect a large part of the design.
    TheCPUWizard is a registered trademark, all rights reserved. (If this post was helpful, please RATE it!)
    2008, 2009,2010
    In theory, there is no difference between theory and practice; in practice there is.

    * Join the fight, refuse to respond to posts that contain code outside of [code] ... [/code] tags. See here for instructions
    * How NOT to post a question here
    * Of course you read this carefully before you posted
    * Need homework help? Read this first

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured