Putting alternate versions of typecasts in your code.

Discussion in 'C++' started by Starx, Jun 16, 2005.

  1. Starx

    Starx Guest

    Say I had the following function:

    template <class numericType>
    double getAverage(const numericType& a, const numericType& b)
    {
    return double(a + b) / 2;
    }

    This will work fine with any types that the double() typecast works on.
    But I'd like it to also work on types that don't have a double()
    typecast, they have a getDouble() member function. First I tried a
    try/catch:

    template <class numericType>
    double getAverage(const numericType& a, const numericType& b)
    {
    try {
    return double(a + b) / 2;
    } catch (...) {
    return (a.getDouble() + b.getDouble()) / 2;
    }
    }

    Turns out that doesn't work, it won't compile if the type doesn't have
    both the getDouble() member function and the double() typecast. Is
    there any way of getting this to work? Any workarounds you could
    suggest?
    Starx, Jun 16, 2005
    #1
    1. Advertising

  2. Starx wrote:
    > Say I had the following function:
    >
    > template <class numericType>
    > double getAverage(const numericType& a, const numericType& b)
    > {
    > return double(a + b) / 2;
    > }
    >
    > This will work fine with any types that the double() typecast works on.
    > But I'd like it to also work on types that don't have a double()
    > typecast, they have a getDouble() member function. First I tried a
    > try/catch:
    >
    > template <class numericType>
    > double getAverage(const numericType& a, const numericType& b)
    > {
    > try {
    > return double(a + b) / 2;
    > } catch (...) {
    > return (a.getDouble() + b.getDouble()) / 2;
    > }
    > }
    >
    > Turns out that doesn't work, it won't compile if the type doesn't have
    > both the getDouble() member function and the double() typecast. Is
    > there any way of getting this to work? Any workarounds you could
    > suggest?


    Implement

    operator double() const;

    in your class that has 'getDouble', in terms of its 'getDouble'. Or
    implement a wrapper class that would use the 'getDouble' and itself
    provide the 'operator double' function. Then specialise your 'getAverage'
    on that wrapper.

    V
    Victor Bazarov, Jun 16, 2005
    #2
    1. Advertising

  3. Starx

    Phlip Guest

    Starx wrote:

    > Turns out that doesn't work, it won't compile if the type doesn't have
    > both the getDouble() member function and the double() typecast. Is
    > there any way of getting this to work? Any workarounds you could
    > suggest?


    You are thinking too hard. (Yes, sometimes C++ requires that, but not now.)

    Firstly, constrain your template (possibly with a trait or policy, if you
    want to think too hard), to only accept types that have a conversion from
    double.

    Add to your types with a getDouble() this:

    operator double () const { return getDouble(); }

    That conversion operator lets a type silently convert to a double when
    challenged by a double context. Now write the template like this:

    template <class numericType>
    double getAverage(const numericType& a, const numericType& b)
    {
    return (a + b) / 2.0;
    }

    The 2.0 is a double, which promotes the other terms to doubles.

    And note that C++ is not an interpreted language, so you can't catch() a
    compiler error.

    --
    Phlip
    http://www.c2.com/cgi/wiki?ZeekLand
    Phlip, Jun 16, 2005
    #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. Saurabh
    Replies:
    6
    Views:
    4,498
    Chris Smith
    May 30, 2004
  2. Chris
    Replies:
    2
    Views:
    667
    Chris
    Mar 4, 2007
  3. Your Encryption Site
    Replies:
    11
    Views:
    689
    Kim André Akerø
    Apr 24, 2008
  4. dorayme
    Replies:
    0
    Views:
    372
    dorayme
    Feb 20, 2009
  5. Replies:
    10
    Views:
    325
    Sean O'Halpin
    Jul 17, 2006
Loading...

Share This Page