Is this ambiguous?

Discussion in 'C++' started by Alan Johnson, Jun 28, 2005.

  1. Alan Johnson

    Alan Johnson Guest

    Consider the following code, with the interesting lines numbered in
    comments:

    class A
    {
    public :

    bool f(int level = 1) // line 5
    { return true ; }

    void f(bool val, int level = 1)
    {}

    } ;

    class B
    {
    private :
    A *a ;
    public :
    bool f(int level = 1) // line 18
    { return a->f(level) ; } // line 19

    void f(bool val, int level = 1)
    { a->f(val, level) ; }
    } ;


    This compiles fine for me (gcc 3.4.4). However, if I make the functions
    on line 5 and line 18 const, then my compiler says the function call on
    line 19 is ambiguous. It makes sense to me why it would be ambiguous
    (level is convertible to bool, and so it might could match f(bool, int)
    since the int parameter has a default value). However, what I don't
    understand is why it is NOT ambiguous without the const. Any insight?

    -Alan
    Alan Johnson, Jun 28, 2005
    #1
    1. Advertising

  2. * Alan Johnson:
    > Consider the following code, with the interesting lines numbered in
    > comments:
    >
    > class A
    > {
    > public :
    >
    > bool f(int level = 1) // line 5
    > { return true ; }
    >
    > void f(bool val, int level = 1)
    > {}
    >
    > } ;
    >
    > class B
    > {
    > private :
    > A *a ;
    > public :
    > bool f(int level = 1) // line 18
    > { return a->f(level) ; } // line 19
    >
    > void f(bool val, int level = 1)
    > { a->f(val, level) ; }
    > } ;
    >
    >
    > This compiles fine for me (gcc 3.4.4). However, if I make the functions
    > on line 5 and line 18 const,


    The one on line 18 doesn't matter.

    > then my compiler says the function call on
    > line 19 is ambiguous. It makes sense to me why it would be ambiguous
    > (level is convertible to bool, and so it might could match f(bool, int)
    > since the int parameter has a default value). However, what I don't
    > understand is why it is NOT ambiguous without the const. Any insight?


    First, be clear that result types are not considered in overload
    resolution, and that making the function on line 18 const does not
    make the object pointed to by 'a' const.

    Without the const on line 5 the compiler has a choice between no
    conversion, and conversion of the argument to bool. The no-conversion
    is clearly better. Hence the first function is selected.

    With the const on line 5 either 'a*' must be converted to
    'a const*', or int must be converted to bool. I don't recall the
    exact rules, but at least there are now two possible conversions. It
    surprises me that the to-const conversion should weight in as much
    as a possible value representation change (and a lossy one, at that!),
    but then, the C++ rules are evolved beasts with lots of non-optimum
    and counter-intuitive consequences.

    --
    A: Because it messes up the order in which people normally read text.
    Q: Why is it such a bad thing?
    A: Top-posting.
    Q: What is the most annoying thing on usenet and in e-mail?
    Alf P. Steinbach, Jun 28, 2005
    #2
    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. Tuukka Toivonen

    Ambiguous type?

    Tuukka Toivonen, Apr 30, 2004, in forum: VHDL
    Replies:
    3
    Views:
    721
    Stefan Frank
    May 3, 2004
  2. Replies:
    2
    Views:
    975
  3. Denon
    Replies:
    2
    Views:
    4,374
    Yuke Liew
    Jan 2, 2004
  4. Nancy.
    Replies:
    3
    Views:
    8,103
    i. Wiin
    Sep 28, 2004
  5. Ben

    Ambiguous match found.

    Ben, Apr 27, 2005, in forum: ASP .Net
    Replies:
    4
    Views:
    7,903
    bond007
    Mar 23, 2010
Loading...

Share This Page