Ambiguity while overloading functions

Discussion in 'C++' started by bintom, Oct 3, 2010.

  1. bintom

    bintom Guest

    The following program uses overloaded functions. It works OK. But if
    the commented function is made part of the program, the compiler
    complains about ambiguity. Is this a case of of integer getting
    promoted to float? Thanks in advance.

    Bintom



    void func(int i)
    { cout << "In func(int)\n\n"; }

    /*
    void func(float f)
    { cout << "In func(float)\n\n"; }
    */

    void func(double d)
    { cout << "In func(double)\n\n"; }

    int main()
    { func(3);
    func(3.3);
    }
    bintom, Oct 3, 2010
    #1
    1. Advertising

  2. bintom

    Sjouke Burry Guest

    bintom wrote:
    > The following program uses overloaded functions. It works OK. But if
    > the commented function is made part of the program, the compiler
    > complains about ambiguity. Is this a case of of integer getting
    > promoted to float? Thanks in advance.
    >
    > Bintom
    >
    >
    >
    > void func(int i)
    > { cout << "In func(int)\n\n"; }
    >
    > /*
    > void func(float f)
    > { cout << "In func(float)\n\n"; }
    > */
    >
    > void func(double d)
    > { cout << "In func(double)\n\n"; }
    >
    > int main()
    > { func(3);
    > func(3.3);
    > }

    You commented out the float function, why?
    Sjouke Burry, Oct 3, 2010
    #2
    1. Advertising

  3. bintom

    Ian Collins Guest

    On 10/ 3/10 03:30 PM, bintom wrote:
    > The following program uses overloaded functions. It works OK. But if
    > the commented function is made part of the program, the compiler
    > complains about ambiguity. Is this a case of of integer getting
    > promoted to float? Thanks in advance.


    Which complier and what is the warning?

    In your posted code there shouldn't be any ambiguity between a floating
    literal and an integer literal.

    > Bintom
    >
    >
    >
    > void func(int i)
    > { cout<< "In func(int)\n\n"; }
    >
    > /*
    > void func(float f)
    > { cout<< "In func(float)\n\n"; }
    > */
    >
    > void func(double d)
    > { cout<< "In func(double)\n\n"; }
    >
    > int main()
    > { func(3);
    > func(3.3);
    > }


    3 is an integer literal, it's type defaults to int. 3.3 is a floating
    literal, which defaults to double. So the int and double overloads will
    be called. func(3.3f) will call the float overload.

    --
    Ian Collins
    Ian Collins, Oct 3, 2010
    #3
  4. bintom <> wrote:
    > void func(int i)
    > { cout << "In func(int)\n\n"; }
    >
    > /*
    > void func(float f)
    > { cout << "In func(float)\n\n"; }
    > */
    >
    > void func(double d)
    > { cout << "In func(double)\n\n"; }
    >
    > int main()
    > { func(3);
    > func(3.3);
    > }


    At least gcc doesn't complain about anything, even with the second
    function uncommented.
    Juha Nieminen, Oct 3, 2010
    #4
  5. bintom

    blackbug Guest

    as said above

    the int changes double by default until you mention something lie 2.2f
    or so.
    blackbug, Oct 3, 2010
    #5
  6. bintom

    Ian Collins Guest

    On 10/ 4/10 02:41 AM, Pete Becker wrote:
    > On 2010-10-02 23:36:04 -0400, Ian Collins said:
    >
    >>
    >> 3 is an integer literal, it's type defaults to int. 3.3 is a floating
    >> literal, which defaults to double.

    >
    > No defaults here; 3 has type int, and 3.3 has type double.


    Isn't that what I said?

    --
    Ian Collins
    Ian Collins, Oct 3, 2010
    #6
  7. bintom

    Ian Collins Guest

    On 10/ 4/10 08:31 AM, Pete Becker wrote:
    > On 2010-10-03 15:13:38 -0400, Ian Collins said:
    >
    >> On 10/ 4/10 02:41 AM, Pete Becker wrote:
    >>> On 2010-10-02 23:36:04 -0400, Ian Collins said:
    >>>
    >>>>
    >>>> 3 is an integer literal, it's type defaults to int. 3.3 is a floating
    >>>> literal, which defaults to double.
    >>>
    >>> No defaults here; 3 has type int, and 3.3 has type double.

    >>
    >> Isn't that what I said?

    >
    > double d = 3;
    >
    > The type of "3" here doesn't default to int, it simply *is* int.
    > Granted, that's a rather subtle point, but defaults can be overridden;
    > the type of "3" can't be. You can use a literal constant with a
    > different type, such as "3U" or "3L", but that's replacing, not overriding.
    >
    > It's an important conceptual difference, but not germane to the specific
    > question under discussion.
    >

    Fair enough, but in the context I was replying to:

    int main()
    { func(3);
    func(3.3);
    }

    my response was correct. The 3 in func(3) is an integer literal and the
    3.3 in func(3.3) is a floating literal. Neither has a type suffix, so
    the default type rules apply.

    --
    Ian Collins
    Ian Collins, Oct 3, 2010
    #7
  8. bintom

    Öö Tiib Guest

    On 3 okt, 22:43, Ian Collins <> wrote:
    > On 10/ 4/10 08:31 AM, Pete Becker wrote:
    >
    > > On 2010-10-03 15:13:38 -0400, Ian Collins said:

    >
    > >> On 10/ 4/10 02:41 AM, Pete Becker wrote:
    > >>> On 2010-10-02 23:36:04 -0400, Ian Collins said:

    >
    > >>>> 3 is an integer literal, it's type defaults to int. 3.3 is a floating
    > >>>> literal, which defaults to double.

    >
    > >>> No defaults here; 3 has type int, and 3.3 has type double.

    >
    > >> Isn't that what I said?

    >
    > > double d = 3;

    >
    > > The type of "3" here doesn't default to int, it simply *is* int.
    > > Granted, that's a rather subtle point, but defaults can be overridden;
    > > the type of "3" can't be. You can use a literal constant with a
    > > different type, such as "3U" or "3L", but that's replacing, not overriding.

    >
    > > It's an important conceptual difference, but not germane to the specific
    > > question under discussion.

    >
    > Fair enough, but in the context I was replying to:
    >
    > int main()
    > { func(3);
    >    func(3.3);
    >
    > }
    >
    > my response was correct.  The 3 in func(3) is an integer literal and the
    > 3.3 in func(3.3) is a floating literal.  Neither has a type suffix, so
    > the default type rules apply.


    So you did mean "defaults to" is same as "without suffix is". Then it
    is not correct. Standard has quite verbose rules how type of integer
    literal without suffix is determined so nothing is considerable as
    default there. Don't worry, I also mix up such terms all the time.
    Öö Tiib, Oct 3, 2010
    #8
  9. bintom

    Ian Collins Guest

    On 10/ 4/10 10:17 AM, Öö Tiib wrote:
    > On 3 okt, 22:43, Ian Collins<> wrote:
    >> On 10/ 4/10 08:31 AM, Pete Becker wrote:
    >>
    >>> On 2010-10-03 15:13:38 -0400, Ian Collins said:

    >>
    >>>> On 10/ 4/10 02:41 AM, Pete Becker wrote:
    >>>>> On 2010-10-02 23:36:04 -0400, Ian Collins said:

    >>
    >>>>>> 3 is an integer literal, it's type defaults to int. 3.3 is a floating
    >>>>>> literal, which defaults to double.

    >>
    >>>>> No defaults here; 3 has type int, and 3.3 has type double.

    >>
    >>>> Isn't that what I said?

    >>
    >>> double d = 3;

    >>
    >>> The type of "3" here doesn't default to int, it simply *is* int.
    >>> Granted, that's a rather subtle point, but defaults can be overridden;
    >>> the type of "3" can't be. You can use a literal constant with a
    >>> different type, such as "3U" or "3L", but that's replacing, not overriding.

    >>
    >>> It's an important conceptual difference, but not germane to the specific
    >>> question under discussion.

    >>
    >> Fair enough, but in the context I was replying to:
    >>
    >> int main()
    >> { func(3);
    >> func(3.3);
    >>
    >> }
    >>
    >> my response was correct. The 3 in func(3) is an integer literal and the
    >> 3.3 in func(3.3) is a floating literal. Neither has a type suffix, so
    >> the default type rules apply.

    >
    > So you did mean "defaults to" is same as "without suffix is". Then it
    > is not correct. Standard has quite verbose rules how type of integer
    > literal without suffix is determined so nothing is considerable as
    > default there. Don't worry, I also mix up such terms all the time.


    The actual wording is:


    "If it is decimal and has no suffix, it has the first of these types
    in which its value can be represented: int, long int; if the value
    cannot be represented as a long int, the behavior is undefined."

    The integer literal 3 can be represented as int, so 3 "without suffix
    is" an int.

    --
    Ian Collins
    Ian Collins, Oct 3, 2010
    #9
  10. Ian Collins wrote:

    > On 10/ 4/10 10:17 AM, Öö Tiib wrote:
    >> On 3 okt, 22:43, Ian Collins<> wrote:
    >>> On 10/ 4/10 08:31 AM, Pete Becker wrote:
    >>>
    >>>> On 2010-10-03 15:13:38 -0400, Ian Collins said:
    >>>
    >>>>> On 10/ 4/10 02:41 AM, Pete Becker wrote:
    >>>>>> On 2010-10-02 23:36:04 -0400, Ian Collins said:
    >>>
    >>>>>>> 3 is an integer literal, it's type defaults to int. 3.3 is a
    >>>>>>> floating literal, which defaults to double.
    >>>
    >>>>>> No defaults here; 3 has type int, and 3.3 has type double.
    >>>
    >>>>> Isn't that what I said?
    >>>
    >>>> double d = 3;
    >>>
    >>>> The type of "3" here doesn't default to int, it simply *is* int.
    >>>> Granted, that's a rather subtle point, but defaults can be overridden;
    >>>> the type of "3" can't be. You can use a literal constant with a
    >>>> different type, such as "3U" or "3L", but that's replacing, not
    >>>> overriding.
    >>>
    >>>> It's an important conceptual difference, but not germane to the
    >>>> specific question under discussion.
    >>>
    >>> Fair enough, but in the context I was replying to:
    >>>
    >>> int main()
    >>> { func(3);
    >>> func(3.3);
    >>>
    >>> }
    >>>
    >>> my response was correct. The 3 in func(3) is an integer literal and the
    >>> 3.3 in func(3.3) is a floating literal. Neither has a type suffix, so
    >>> the default type rules apply.

    >>
    >> So you did mean "defaults to" is same as "without suffix is". Then it
    >> is not correct. Standard has quite verbose rules how type of integer
    >> literal without suffix is determined so nothing is considerable as
    >> default there. Don't worry, I also mix up such terms all the time.

    >
    > The actual wording is:
    >
    >
    > "If it is decimal and has no suffix, it has the first of these types
    > in which its value can be represented: int, long int; if the value
    > cannot be represented as a long int, the behavior is undefined."
    >


    No, your use of "default" is wrong. I agree with Pete here. There is no
    'default'. That's a keyword.
    Johannes Schaub (litb), Oct 10, 2010
    #10
    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. Xiangliang Meng
    Replies:
    1
    Views:
    1,576
    Victor Bazarov
    Jun 21, 2004
  2. Code4u
    Replies:
    7
    Views:
    405
    Code4u
    Jul 11, 2005
  3. , India
    Replies:
    2
    Views:
    252
    Alan Johnson
    Jun 25, 2007
  4. Adam Nielsen
    Replies:
    3
    Views:
    401
    Adam Nielsen
    Sep 27, 2007
  5. Fred Zwarts
    Replies:
    8
    Views:
    836
    Michael Doubez
    Jun 16, 2009
Loading...

Share This Page