Templates question

Discussion in 'C++' started by Asif Zaidi, Feb 16, 2010.

  1. Asif Zaidi

    Asif Zaidi Guest

    Hi:

    Learning templates now...

    I can code a template for min(1,2) and min(1.0, 2.3) using the same
    class definition.

    I am trying to do min(1, 0.8) and this is where I am stuck.

    I am not getting the definition right for my_min function - I tried
    googling on various variations of templates but I probably don't know
    the right term to google for.

    My code is as follows. My reasoning is, since my_min will have 2
    different type arguments, I need (T, U) and my return type is U (which
    is 2nd arg).

    I am getting compile errors (See ERROR below). Any help is
    appreciated.


    ==============================================
    #include <iostream>
    using namespace std;

    template <class T, class U>
    struct MinPromotion;


    template <>
    struct MinPromotion<int, double>
    {
    typedef double PromotedType;
    };

    // ERROR
    template<class T, class U>
    const U & <class MinPromotion<T, U>::promotedType> my_min (const T
    &x, const U &y)
    {
    return x < y ? x : y;
    }



    template<class T>
    const T &my_min(const T &x, const T &y)
    { return x < y ? x : y; }



    int main()
    {
    cout << my_min(1,2) << endl;
    cout << my_min(2.2, 3.2) << endl;
    // cout << my_min(1, 0.7) << endl;
    }

    =================
     
    Asif Zaidi, Feb 16, 2010
    #1
    1. Advertising

  2. Asif Zaidi wrote:
    > Hi:
    >
    > Learning templates now...
    >
    > I can code a template for min(1,2) and min(1.0, 2.3) using the same
    > class definition.
    >
    > I am trying to do min(1, 0.8) and this is where I am stuck.
    >
    > I am not getting the definition right for my_min function - I tried
    > googling on various variations of templates but I probably don't know
    > the right term to google for.
    >
    > My code is as follows. My reasoning is, since my_min will have 2
    > different type arguments, I need (T, U) and my return type is U (which
    > is 2nd arg).
    >
    > I am getting compile errors (See ERROR below). Any help is
    > appreciated.
    >
    >
    > ==============================================
    > #include <iostream>
    > using namespace std;
    >
    > template <class T, class U>
    > struct MinPromotion;
    >
    >
    > template <>
    > struct MinPromotion<int, double>
    > {
    > typedef double PromotedType;
    > };
    >
    > // ERROR
    > template<class T, class U>
    > const U & <class MinPromotion<T, U>::promotedType> my_min (const T
    > &x, const U &y)
    > {
    > return x < y ? x : y;
    > }
    >


    What you wrote there makes no sense. This:
    template<class T, class U>
    const typename MinPromotion< T, U >::promotedType& my_min (const T &x,
    const U &y)
    {
    return x < y ? x : y;
    }
    compiles, and it looks like it satisfies your requirements, but it is an
    undefined behaviour to return a reference to different types. For that
    reason, this would be ok:
    template<class T, class U>
    const typename MinPromotion< T, U >::promotedType my_min (const T &x,
    const U &y)
    {
    return x < y ? x : y;
    }

    BTW why don't you use std::min?

    >
    >
    > template<class T>
    > const T &my_min(const T &x, const T &y)
    > { return x < y ? x : y; }
    >
    >
    >
    > int main()
    > {
    > cout << my_min(1,2) << endl;
    > cout << my_min(2.2, 3.2) << endl;
    > // cout << my_min(1, 0.7) << endl;
    > }
    >
    > =================
     
    Vladimir Jovic, Feb 16, 2010
    #2
    1. Advertising

  3. Be careful with this solution. It assumes that you compare types for
    which the operator< is defined.
    You should perform a static check before performing the comparison.

    On Feb 16, 9:56 am, Vladimir Jovic <> wrote:
    > Asif Zaidi wrote:
    > > Hi:

    >
    > > Learning templates now...

    >
    > > I can code a template for min(1,2) and min(1.0, 2.3) using the same
    > > class definition.

    >
    > > I am trying to do min(1, 0.8) and this is where I am stuck.

    >
    > > I am not getting the definition right for my_min function - I tried
    > > googling on various variations of templates but I probably don't know
    > > the right term to google for.

    >
    > > My code is as follows. My reasoning is, since my_min will have 2
    > > different type arguments, I need (T, U) and my return type is U (which
    > > is 2nd arg).

    >
    > > I am getting compile errors (See ERROR below). Any help is
    > > appreciated.

    >
    > > ==============================================
    > > #include <iostream>
    > > using namespace std;

    >
    > > template <class T, class U>
    > > struct MinPromotion;

    >
    > > template <>
    > > struct MinPromotion<int, double>
    > > {
    > >     typedef double PromotedType;
    > > };

    >
    > > // ERROR
    > > template<class  T, class U>
    > > const U & <class  MinPromotion<T, U>::promotedType> my_min (const T
    > > &x, const U &y)
    > > {
    > > return x < y ? x : y;
    > > }

    >
    > What you wrote there makes no sense. This:
    > template<class  T, class U>
    > const typename  MinPromotion< T, U >::promotedType&  my_min (const T &x,
    > const U &y)
    > {
    > return x < y ? x : y;}
    >
    > compiles, and it looks like it satisfies your requirements, but it is an
    > undefined behaviour to return a reference to different types. For that
    > reason, this would be ok:
    > template<class  T, class U>
    > const typename  MinPromotion< T, U >::promotedType  my_min (const T &x,
    > const U &y)
    > {
    > return x < y ? x : y;
    >
    > }
    >
    > BTW why don't you use std::min?
    >
    >
    >
    >
    >
    > > template<class T>
    > > const T &my_min(const T &x, const T &y)
    > > { return x < y ? x : y; }

    >
    > > int main()
    > > {
    > >     cout << my_min(1,2) << endl;
    > >     cout << my_min(2.2, 3.2) << endl;
    > >     // cout << my_min(1, 0.7) << endl;
    > > }

    >
    > > =================
     
    Clement Cousin, Feb 16, 2010
    #3
    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. Fred
    Replies:
    1
    Views:
    630
    Neredbojias
    Sep 26, 2005
  2. John Harrison

    using templates in templates

    John Harrison, Jul 31, 2003, in forum: C++
    Replies:
    8
    Views:
    398
    Torsten Curdt
    Jul 31, 2003
  3. JKop
    Replies:
    3
    Views:
    505
  4. Tom McCallum

    Templates within templates

    Tom McCallum, Aug 4, 2004, in forum: C++
    Replies:
    2
    Views:
    378
    tom_usenet
    Aug 4, 2004
  5. recover
    Replies:
    2
    Views:
    845
    recover
    Jul 25, 2006
Loading...

Share This Page