function overloading: direct-match vs trivial-conversion

Discussion in 'C++' started by subramanian100in@yahoo.com, India, Apr 13, 2008.

  1. , India

    , India Guest

    Suppose T is a type.

    Consider the two functions:

    void fn(T& first)
    {
    ....
    }

    void fn(T const & second)
    {
    ....
    }

    Suppose I have the declaration
    T obj;

    If I call fn(obj), then the function, void fn(T& first) will be
    called. This is because:
    When both
    1) 'T' to 'T&' (this is a direct match)
    2) 'T' to 'T const &' (this involves trivial conversion)
    are present, direct match takes higher precedence. If direct match is
    not found, only then trivial conversion is considered. That is, if the
    first function void fn(T& first) is not present, only then the second
    function void fn(T const & second) will be called.

    Is my above understanding correct ?

    Kindly clarify.

    Thanks
    V.Subramanian
     
    , India, Apr 13, 2008
    #1
    1. Advertisements

  2. , India

    Looney Guest

    On Apr 13, 3:26 pm, ", India"
    <> wrote:
    > Suppose T is a type.
    >
    > Consider the two functions:
    >
    > void fn(T& first)
    > {
    > ...
    >
    > }
    >
    > void fn(T const & second)
    > {
    > ...
    >
    > }
    >
    > Suppose I have the declaration
    > T obj;
    >
    > If I call fn(obj), then the function, void fn(T& first) will be
    > called. This is because:
    > When both
    > 1) 'T' to 'T&' (this is a direct match)
    > 2) 'T' to 'T const &' (this involves trivial conversion)
    > are present, direct match takes higher precedence. If direct match is
    > not found, only then trivial conversion is considered. That is, if the
    > first function void fn(T& first) is not present, only then the second
    > function void fn(T const & second) will be called.
    >
    > Is my above understanding correct ?
    >
    > Kindly clarify.
    >
    > Thanks
    > V.Subramanian


    yes that is correct .
    check out http://accu.org/index.php/journals/268
    for a details
     
    Looney, Apr 13, 2008
    #2
    1. Advertisements

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. hiwa
    Replies:
    0
    Views:
    808
  2. christopher diggins
    Replies:
    0
    Views:
    364
    christopher diggins
    May 16, 2005
  3. Iyer, Prasad C

    Overloading __init__ & Function overloading

    Iyer, Prasad C, Sep 30, 2005, in forum: Python
    Replies:
    3
    Views:
    6,651
    Fredrik Lundh
    Sep 30, 2005
  4. Iyer, Prasad C
    Replies:
    4
    Views:
    776
    John J. Lee
    Sep 30, 2005
  5. baibaichen

    trivial or non-trivial object

    baibaichen, Jan 12, 2006, in forum: C++
    Replies:
    3
    Views:
    1,284
    osmium
    Jan 12, 2006
  6. Ken Fine
    Replies:
    1
    Views:
    799
    Steve C. Orr [MCSD, MVP, CSM, ASP Insider]
    Jul 31, 2007
  7. Tore Halvorsen
    Replies:
    17
    Views:
    324
    Robert Klemme
    Sep 19, 2004
  8. Replies:
    2
    Views:
    651
Loading...