generic functions(template functions)

Discussion in 'C++' started by Tony Johansson, Aug 16, 2005.

  1. Hello Experts!

    Assume I have one function that is a template function called add and one
    concrete function that also have the name add is it any point to make a
    concrete function add in this example?

    template <typename T>
    T add(T rh, T lh)
    { return rh+lh; }

    template <typename T>
    int add(int rh, int lh)
    { return rh+lh; }
    int main()
    {
    int i=1, j=2;
    cout << "result " << add<int>(i,j) << endl;

    return 0;
    }

    //Many thanks

    //Tony
     
    Tony Johansson, Aug 16, 2005
    #1
    1. Advertising

  2. Tony Johansson

    Srini Guest

    > Hello Experts!
    >
    > Assume I have one function that is a template function called add and one
    > concrete function that also have the name add is it any point to make a
    > concrete function add in this example?
    >
    > template <typename T>
    > T add(T rh, T lh)
    > { return rh+lh; }
    >
    > template <typename T>
    > int add(int rh, int lh)
    > { return rh+lh; }
    > int main()
    > {
    > int i=1, j=2;
    > cout << "result " << add<int>(i,j) << endl;
    > return 0;
    >
    > }


    In the above example there's no point for the template
    specialization(That's the term you'd want to use). In general you'd
    want to specialize a function template when you want to have different
    behavior for particular types.

    Srini
     
    Srini, Aug 16, 2005
    #2
    1. Advertising

  3. Tony Johansson

    benben Guest

    "Srini" <> wrote in message
    news:...
    >> Hello Experts!
    >>
    >> Assume I have one function that is a template function called add and one
    >> concrete function that also have the name add is it any point to make a
    >> concrete function add in this example?
    >>
    >> template <typename T>
    >> T add(T rh, T lh)
    >> { return rh+lh; }
    >>
    >> template <typename T>
    >> int add(int rh, int lh)
    >> { return rh+lh; }
    >> int main()
    >> {
    >> int i=1, j=2;
    >> cout << "result " << add<int>(i,j) << endl;
    >> return 0;
    >>
    >> }

    >
    > In the above example there's no point for the template
    > specialization(That's the term you'd want to use). In general you'd
    > want to specialize a function template when you want to have different
    > behavior for particular types.
    >
    > Srini
    >


    The code by Tony didn't look to me like template specialization. A full
    specialization of function template add<> would look like:

    template <>
    int add(int rh, int lh)
    {
    return rh + lh;
    }
     
    benben, Aug 16, 2005
    #3
  4. Tony Johansson

    Srini Guest

    > The code by Tony didn't look to me like template specialization. A full
    > specialization of function template add<> would look like:
    >
    > template <>
    > int add(int rh, int lh)
    > {
    > return rh + lh;
    > }


    Yeah - even I thought so. But I checked compiling both forms (What OP
    has given and what you've given) and both gave same results. I thought
    that specialization was what the OP meant in his post.
     
    Srini, Aug 16, 2005
    #4
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Murat Tasan
    Replies:
    1
    Views:
    8,053
    Chaitanya
    Feb 3, 2009
  2. Replies:
    2
    Views:
    437
  3. johanatan
    Replies:
    6
    Views:
    386
    James Kanze
    Feb 19, 2008
  4. Hicham Mouline
    Replies:
    0
    Views:
    435
    Hicham Mouline
    Apr 23, 2009
  5. Hicham Mouline
    Replies:
    1
    Views:
    421
    Michael DOUBEZ
    Apr 24, 2009
Loading...

Share This Page