Overloading vs. specialization of function templates

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Dave Theese

    Overloading vs. specialization of function templates


    Hello all,

    I'm trying to get a grasp of the difference between specializing a function
    template and overloading it. The example below has a primary template, a
    specialization and an overload. Note that the overload is identical to the
    specialization except, of course, for the missing "template <>".

    I don't know if my questions will be a bit too broad or not, but I thought
    I'd give it shot... When is overloading preferable to specialization? When
    is specialization preferable to overloading? What is the intended
    conceptual difference between the two? Any other guidance on other things I
    need to know but don't know enough yet to even ask?

    Thanks everyone - this group has been an invaluable resource to me and I
    sure appreciate the time of those who have so generously assisted me!

    Thanks,
    Dave


    #include <iostream>

    using namespace std;

    struct foo
    {
    int data;

    bool operator<(const foo &rhs) const
    {
    return data < rhs.data;
    }
    };

    template <typename T>
    const T &my_max(cons t T &a, const T &b)
    {
    cout << "Point 1" << endl;
    return (a < b) ? b : a;
    }

    template <>
    const foo &my_max(cons t foo &a, const foo &b)
    {
    cout << "Point 2" << endl;
    return (a < b) ? b : a;
    }

    const foo &my_max(cons t foo &a, const foo &b)
    {
    cout << "Point 3" << endl;
    return (a < b) ? b : a;
    }

    int main()
    {
    foo a = {5};
    foo b = {10};

    // Yields "Point 3" as non-templates are
    // preferred.
    cout << my_max(a, b).data << endl;

    return 0;
    }


  • Senthilvel Samatharman

    #2
    Re: Overloading vs. specialization of function templates

    Dave Theese wrote:
    [color=blue]
    > Hello all,
    >
    > I'm trying to get a grasp of the difference between specializing a function
    > template and overloading it. The example below has a primary template, a
    > specialization and an overload. Note that the overload is identical to the
    > specialization except, of course, for the missing "template <>".
    >
    > I don't know if my questions will be a bit too broad or not, but I thought
    > I'd give it shot... When is overloading preferable to specialization? When
    > is specialization preferable to overloading? What is the intended
    > conceptual difference between the two? Any other guidance on other things I
    > need to know but don't know enough yet to even ask?[/color]

    I had the same question sometime back and this is the reply from Bjarne
    Stroustrup....

    "For functions, you don't have to. Overloading will do. For classes, you don't
    have an alternative. Note that when you specialize the specialzation doesn't
    affect overload resolution rules, when you overload with a non-specialization,
    overload resolution will prefer the non-specialization. "
    [color=blue]
    >
    >
    > Thanks everyone - this group has been an invaluable resource to me and I
    > sure appreciate the time of those who have so generously assisted me!
    >
    > Thanks,
    > Dave
    >
    > #include <iostream>
    >
    > using namespace std;
    >
    > struct foo
    > {
    > int data;
    >
    > bool operator<(const foo &rhs) const
    > {
    > return data < rhs.data;
    > }
    > };
    >
    > template <typename T>
    > const T &my_max(cons t T &a, const T &b)
    > {
    > cout << "Point 1" << endl;
    > return (a < b) ? b : a;
    > }
    >
    > template <>
    > const foo &my_max(cons t foo &a, const foo &b)
    > {
    > cout << "Point 2" << endl;
    > return (a < b) ? b : a;
    > }
    >
    > const foo &my_max(cons t foo &a, const foo &b)
    > {
    > cout << "Point 3" << endl;
    > return (a < b) ? b : a;
    > }
    >
    > int main()
    > {
    > foo a = {5};
    > foo b = {10};
    >
    > // Yields "Point 3" as non-templates are
    > // preferred.
    > cout << my_max(a, b).data << endl;
    >
    > return 0;
    > }[/color]

    Comment

    • Mike Wahler

      #3
      Re: Overloading vs. specialization of function templates


      Senthilvel Samatharman <Samatharman.Se nthilvel@adcc.a lcatel.be> wrote in
      message news:3F5DDC7D.F 96BDD20@adcc.al catel.be...[color=blue]
      > Dave Theese wrote:
      >[color=green]
      > > Hello all,
      > >
      > > I'm trying to get a grasp of the difference between specializing a[/color][/color]
      function[color=blue][color=green]
      > > template and overloading it. The example below has a primary template,[/color][/color]
      a[color=blue][color=green]
      > > specialization and an overload. Note that the overload is identical to[/color][/color]
      the[color=blue][color=green]
      > > specialization except, of course, for the missing "template <>".
      > >
      > > I don't know if my questions will be a bit too broad or not, but I[/color][/color]
      thought[color=blue][color=green]
      > > I'd give it shot... When is overloading preferable to specialization?[/color][/color]
      When[color=blue][color=green]
      > > is specialization preferable to overloading? What is the intended
      > > conceptual difference between the two? Any other guidance on other[/color][/color]
      things I[color=blue][color=green]
      > > need to know but don't know enough yet to even ask?[/color]
      >
      > I had the same question sometime back and this is the reply from Bjarne
      > Stroustrup....
      >
      > "For functions, you don't have to. Overloading will do. For classes, you[/color]
      don't[color=blue]
      > have an alternative. Note that when you specialize the specialzation[/color]
      doesn't[color=blue]
      > affect overload resolution rules, when you overload with a[/color]
      non-specialization,[color=blue]
      > overload resolution will prefer the non-specialization. "[/color]

      Vandevoordes & Josuttis' recent book "C++ Templates"
      imo covers this and other template issues quite well.

      I must admit it will take me at least a few passes through this
      great book to absorb everything therein. :-)



      -Mike



      Comment

      • Govindan

        #4
        Re: Overloading vs. specialization of function templates


        "Dave Theese" <cheeser_1998@y ahoo.com> wrote in message
        news:KUa7b.1466 5$QT5.9944@fed1 read02...[color=blue]
        >
        > Hello all,
        >
        > I'm trying to get a grasp of the difference between specializing a[/color]
        function[color=blue]
        > template and overloading it. The example below has a primary template, a
        > specialization and an overload. Note that the overload is identical to[/color]
        the[color=blue]
        > specialization except, of course, for the missing "template <>".
        >
        > I don't know if my questions will be a bit too broad or not, but I thought
        > I'd give it shot... When is overloading preferable to specialization?[/color]
        When[color=blue]
        > is specialization preferable to overloading? What is the intended
        > conceptual difference between the two? Any other guidance on other things[/color]
        I[color=blue]
        > need to know but don't know enough yet to even ask?
        >
        > Thanks everyone - this group has been an invaluable resource to me and I
        > sure appreciate the time of those who have so generously assisted me!
        >
        > Thanks,
        > Dave
        >
        >
        > #include <iostream>
        >
        > using namespace std;
        >
        > struct foo
        > {
        > int data;
        >
        > bool operator<(const foo &rhs) const
        > {
        > return data < rhs.data;
        > }
        > };
        >
        > template <typename T>
        > const T &my_max(cons t T &a, const T &b)
        > {
        > cout << "Point 1" << endl;
        > return (a < b) ? b : a;
        > }
        >
        > template <>
        > const foo &my_max(cons t foo &a, const foo &b)
        > {
        > cout << "Point 2" << endl;
        > return (a < b) ? b : a;
        > }
        >
        > const foo &my_max(cons t foo &a, const foo &b)
        > {
        > cout << "Point 3" << endl;
        > return (a < b) ? b : a;
        > }
        >
        > int main()
        > {
        > foo a = {5};
        > foo b = {10};
        >
        > // Yields "Point 3" as non-templates are
        > // preferred.
        > cout << my_max(a, b).data << endl;
        >
        > return 0;
        > }
        >
        >[/color]


        I am not so clear about your question but here goes. I am using simple
        examples
        to explain the concepts.

        Use composition ( has-a relationship,em bedded objects ) over inheritance
        (i.e specialisation of a base class).
        If there are many related classes differing in the parameters only by the
        type, then use templates.
        Use operator or function overloading as a last resort.
        As an example the sign plus can mean binary addition eg 4 + 7 or string
        concatenation or adding
        two graphic figures together depending on your program or class.
        Thus in this case the plus sign token is an overloaded operator.

        As an example for function overloading, lets say for a function
        AverageOf3Numbe rs(param1, param2, param3 ,...)
        where average can mean mean or median.
        The AverageOf3Numbe rs( totalof3Numbers ) function divides the param1 by 3 to
        get the average, as in mean case;
        in the other case,the median interpretation of average, all 3 numbers are
        passed into the function as in,
        AverageOf3Numbe rs(3 , 5, 8) function does a integer comparison and gives 5
        as the median(average) .

        That is in your class definition, there are two AverageOf3Numbe rs( ) methods
        declared.

        However, if you just want to have one interpreation of the meaning of
        average , as mean, but the types of the parameters differ,
        use a template.
        AverageOf3Numbe rs(integer1, integer2, integer3) ; AverageOf3Numbe rs(float1,
        float2, float3) etc
        Use a template class.
        As an example of operator-loading, You can write your own complex nos. class
        to add , subtract, divide and multiply
        complex nos. In this class, all "+", "-", "*" and "/" will be overloaded ,
        the methods will be written by you.

        First check with a C++ standard book and a STL library book before writing
        your own classes. The STL is template-based
        and already has many useful methods , functions,funct ors you can use etc

        Regards,
        Govin





        Comment

        • tom_usenet

          #5
          Re: Overloading vs. specialization of function templates

          On Mon, 8 Sep 2003 19:30:51 -0700, "Dave Theese"
          <cheeser_1998@y ahoo.com> wrote:
          [color=blue]
          >
          >Hello all,
          >
          >I'm trying to get a grasp of the difference between specializing a function
          >template and overloading it. The example below has a primary template, a
          >specializati on and an overload. Note that the overload is identical to the
          >specializati on except, of course, for the missing "template <>".
          >
          >I don't know if my questions will be a bit too broad or not, but I thought
          >I'd give it shot... When is overloading preferable to specialization?[/color]

          Non-template overloads can be useful if you want the function to be
          chosen in the face of conversions and non-exact parameter matches.

          Also, because partial specialization doesn't exist for function
          templates, you have to use overloading when you need a special
          implementation for a subset of cases of a template.

          When[color=blue]
          >is specialization preferable to overloading?[/color]

          When you just want to provide a special implementation for a
          particular type, but don't want to effect overload resolution.

          What is the intended[color=blue]
          >conceptual difference between the two?[/color]

          An overload adds a new function, a specialization just specializes an
          implementation of a function you already have.

          Any other guidance on other things I[color=blue]
          >need to know but don't know enough yet to even ask?[/color]

          There's a bit more information here:


          and a ludicrous amount of information about all sorts of C++ things
          here:


          Tom

          Comment

          Working...