some problems about exact match in the overload resolution.

Discussion in 'C++' started by Wayne Shu, Jul 29, 2007.

  1. Wayne Shu

    Wayne Shu Guest

    Hi everyone.

    In the following program, foo is an ambiguous call.

    #include <iostream>
    using namespace std;

    void foo(int *);
    void foo(int (&)[5]);

    int main()
    {
    int arr[5] = {0, 1, 2, 3, 4};

    foo(arr);

    return 0;
    }

    why?
    void foo(int (&)[5]); is an exact match
    void foo(int (&)[5]); require an array to pointer conversion.

    Regards.
     
    Wayne Shu, Jul 29, 2007
    #1
    1. Advertising

  2. Wayne Shu wrote:
    > Hi everyone.
    >
    > In the following program, foo is an ambiguous call.
    >
    > #include <iostream>
    > using namespace std;
    >
    > void foo(int *);
    > void foo(int (&)[5]);
    >
    > int main()
    > {
    > int arr[5] = {0, 1, 2, 3, 4};
    >
    > foo(arr);
    >
    > return 0;
    > }
    >
    > why?
    > void foo(int (&)[5]); is an exact match
    > void foo(int (&)[5]); require an array to pointer conversion.


    You mean foo(int*) requires the conversion...

    No, foo(int(&)[5]) is not an exact match. It requires binding
    of a reference. An exact match would be a function with an array
    as the argument, which is impossible.

    V
    --
    Please remove capital 'A's when replying by e-mail
    I do not respond to top-posted replies, please don't ask
     
    Victor Bazarov, Jul 29, 2007
    #2
    1. Advertising

  3. Wayne Shu

    James Kanze Guest

    On Jul 29, 2:58 pm, Wayne Shu <> wrote:
    > In the following program, foo is an ambiguous call.


    > #include <iostream>
    > using namespace std;


    > void foo(int *);
    > void foo(int (&)[5]);


    > int main()
    > {
    > int arr[5] = {0, 1, 2, 3, 4};
    >
    > foo(arr);
    >
    > return 0;
    >
    > }


    > why?
    > void foo(int (&)[5]); is an exact match


    Actually, it's what the standard calls an lvalue transformation.

    > void foo(int (&)[5]); require an array to pointer conversion.


    You mean "foo( int* )", of course. For purposes of overload
    resolution, the standard considers it an lvalue transformation
    as well; for purposes of ranking, both are considered exact
    matches. There are additional considerations which can be taken
    into account, but none apply here.

    --
    James Kanze (Gabi Software) email:
    Conseils en informatique orientée objet/
    Beratung in objektorientierter Datenverarbeitung
    9 place Sémard, 78210 St.-Cyr-l'École, France, +33 (0)1 30 23 00 34
     
    James Kanze, Jul 29, 2007
    #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. Piotre Ugrumov
    Replies:
    3
    Views:
    375
    Nick Hounsome
    Jan 25, 2004
  2. Andy
    Replies:
    2
    Views:
    310
    John Harrison
    Sep 10, 2005
  3. Neelesh Bodas

    Exact Match and Identity conversion

    Neelesh Bodas, Aug 8, 2007, in forum: C++
    Replies:
    17
    Views:
    714
  4. Mr.SpOOn
    Replies:
    4
    Views:
    509
    Lawrence D'Oliveiro
    Nov 4, 2008
  5. Ying-Chieh Liao

    function overload (not operator overload)

    Ying-Chieh Liao, Oct 11, 2004, in forum: Perl Misc
    Replies:
    3
    Views:
    255
    Sherm Pendley
    Oct 11, 2004
Loading...

Share This Page