operator*(Foo) and operator*(int) const: ISO C++ says that these are ambiguous:

Discussion in 'C++' started by Alex Vinokur, Nov 26, 2004.

  1. Alex Vinokur

    Alex Vinokur Guest

    Why is it ambiguous?

    ------ foo.cpp ------
    struct Foo
    {
    Foo operator* (Foo) { return Foo(); }
    Foo operator* (int) const { return Foo(); }
    Foo () {}
    Foo (int) {}
    };

    int main ()
    {
    Foo foo1;
    Foo foo2;
    foo1 = foo2 * 10;
    return 0;
    }
    ---------------------


    ------ Compilation ------

    $ gpp foo.cpp
    foo.cpp: In function `int main()':
    foo.cpp:13: error: ISO C++ says that these are ambiguous, even though the worst
    conversion for the first is better than the worst conversion for the second:
    foo.cpp:4: note: candidate 1: Foo Foo::eek:perator*(int) const
    foo.cpp:3: note: candidate 2: Foo Foo::eek:perator*(Foo)

    -------------------------

    P.S. If we are using 'operator*(int)' instead of 'operator*(int) const' there is no ambiguity.

    --
    Alex Vinokur
    email: alex DOT vinokur AT gmail DOT com
    http://mathforum.org/library/view/10978.html
    http://sourceforge.net/users/alexvn
    Alex Vinokur, Nov 26, 2004
    #1
    1. Advertising

  2. "Alex Vinokur" <> skrev i en meddelelse
    news:...
    > Why is it ambiguous?
    >
    > ------ foo.cpp ------
    > struct Foo
    > {
    > Foo operator* (Foo) { return Foo(); }
    > Foo operator* (int) const { return Foo(); }
    > Foo () {}
    > Foo (int) {}
    > };
    >
    > int main ()
    > {
    > Foo foo1;
    > Foo foo2;
    > foo1 = foo2 * 10;
    > return 0;
    > }
    > ---------------------
    >
    >
    > ------ Compilation ------
    >
    > $ gpp foo.cpp
    > foo.cpp: In function `int main()':
    > foo.cpp:13: error: ISO C++ says that these are ambiguous, even though the
    > worst
    > conversion for the first is better than the worst conversion for the
    > second:
    > foo.cpp:4: note: candidate 1: Foo Foo::eek:perator*(int) const
    > foo.cpp:3: note: candidate 2: Foo Foo::eek:perator*(Foo)
    >
    > -------------------------
    >
    > P.S. If we are using 'operator*(int)' instead of 'operator*(int) const'
    > there is no ambiguity.


    For the first case, you need to convert 10 to a Foo, for the second you need
    to convert foo2 from Foo to Foo const. Both conversions are deemed equal.

    /Peter

    >
    > --
    > Alex Vinokur
    > email: alex DOT vinokur AT gmail DOT com
    > http://mathforum.org/library/view/10978.html
    > http://sourceforge.net/users/alexvn
    >
    >
    Peter Koch Larsen, Nov 26, 2004
    #2
    1. Advertising

  3. On Fri, 26 Nov 2004 15:15:13 +0200, "Alex Vinokur"
    <> wrote:

    >Why is it ambiguous?
    >
    >------ foo.cpp ------
    >struct Foo
    >{
    > Foo operator* (Foo) { return Foo(); }
    > Foo operator* (int) const { return Foo(); }
    > Foo () {}
    > Foo (int) {}
    >};
    >
    >int main ()
    >{
    >Foo foo1;
    >Foo foo2;
    > foo1 = foo2 * 10;
    > return 0;
    >}
    >---------------------
    >
    >
    >------ Compilation ------
    >
    >$ gpp foo.cpp
    >foo.cpp: In function `int main()':
    >foo.cpp:13: error: ISO C++ says that these are ambiguous, even though the worst
    >conversion for the first is better than the worst conversion for the second:
    >foo.cpp:4: note: candidate 1: Foo Foo::eek:perator*(int) const
    >foo.cpp:3: note: candidate 2: Foo Foo::eek:perator*(Foo)
    >
    >-------------------------
    >
    >P.S. If we are using 'operator*(int)' instead of 'operator*(int) const' there is no ambiguity.


    Have you tried making Foo::Foo(int) explicit?

    --
    Bob Hairgrove
    Bob Hairgrove, Nov 26, 2004
    #3
  4. Alex Vinokur

    Alex Vinokur Guest

    "Bob Hairgrove" <> wrote in message news:...
    > On Fri, 26 Nov 2004 15:15:13 +0200, "Alex Vinokur"
    > <> wrote:
    >
    > >Why is it ambiguous?
    > >
    > >------ foo.cpp ------
    > >struct Foo
    > >{
    > > Foo operator* (Foo) { return Foo(); }
    > > Foo operator* (int) const { return Foo(); }
    > > Foo () {}
    > > Foo (int) {}
    > >};
    > >
    > >int main ()
    > >{
    > >Foo foo1;
    > >Foo foo2;
    > > foo1 = foo2 * 10;
    > > return 0;
    > >}
    > >---------------------
    > >
    > >
    > >------ Compilation ------
    > >
    > >$ gpp foo.cpp
    > >foo.cpp: In function `int main()':
    > >foo.cpp:13: error: ISO C++ says that these are ambiguous, even though the worst
    > >conversion for the first is better than the worst conversion for the second:
    > >foo.cpp:4: note: candidate 1: Foo Foo::eek:perator*(int) const
    > >foo.cpp:3: note: candidate 2: Foo Foo::eek:perator*(Foo)
    > >
    > >-------------------------
    > >
    > >P.S. If we are using 'operator*(int)' instead of 'operator*(int) const' there is no ambiguity.

    >
    > Have you tried making Foo::Foo(int) explicit?
    >

    [snip]

    Thanks.

    Compiler has no problem with code below.

    struct Foo
    {
    Foo operator* (Foo) { return Foo(); }
    Foo operator* (int) const { return Foo(); }
    Foo () {}
    explicit Foo (int) {}
    };

    int main ()
    {
    Foo foo1;
    Foo foo2;
    foo1 = foo2 * 10;
    return 0;
    }

    So, Foo::Foo (int) was implicitly used in the original program (?).
    Where?

    If we are using 'operator*(int)' instead of 'operator*(int) const' in the _original_ program a compiler has no problem too. Why?


    --
    Alex Vinokur
    email: alex DOT vinokur AT gmail DOT com
    http://mathforum.org/library/view/10978.html
    http://sourceforge.net/users/alexvn
    Alex Vinokur, Nov 26, 2004
    #4
  5. "Alex Vinokur" <> skrev i en meddelelse
    news:...
    >
    > "Bob Hairgrove" <> wrote in message
    > news:...
    >> On Fri, 26 Nov 2004 15:15:13 +0200, "Alex Vinokur"
    >> <> wrote:
    >>
    >> >Why is it ambiguous?
    >> >
    >> >------ foo.cpp ------
    >> >struct Foo
    >> >{
    >> > Foo operator* (Foo) { return Foo(); }
    >> > Foo operator* (int) const { return Foo(); }
    >> > Foo () {}
    >> > Foo (int) {}
    >> >};
    >> >
    >> >int main ()
    >> >{
    >> >Foo foo1;
    >> >Foo foo2;
    >> > foo1 = foo2 * 10;
    >> > return 0;
    >> >}
    >> >---------------------
    >> >
    >> >
    >> >------ Compilation ------
    >> >
    >> >$ gpp foo.cpp
    >> >foo.cpp: In function `int main()':
    >> >foo.cpp:13: error: ISO C++ says that these are ambiguous, even though
    >> >the worst
    >> >conversion for the first is better than the worst conversion for the
    >> >second:
    >> >foo.cpp:4: note: candidate 1: Foo Foo::eek:perator*(int) const
    >> >foo.cpp:3: note: candidate 2: Foo Foo::eek:perator*(Foo)
    >> >
    >> >-------------------------
    >> >
    >> >P.S. If we are using 'operator*(int)' instead of 'operator*(int) const'
    >> >there is no ambiguity.

    >>
    >> Have you tried making Foo::Foo(int) explicit?
    >>

    > [snip]
    >
    > Thanks.
    >
    > Compiler has no problem with code below.
    >
    > struct Foo
    > {
    > Foo operator* (Foo) { return Foo(); }
    > Foo operator* (int) const { return Foo(); }
    > Foo () {}
    > explicit Foo (int) {}
    > };
    >
    > int main ()
    > {
    > Foo foo1;
    > Foo foo2;
    > foo1 = foo2 * 10;
    > return 0;
    > }
    >
    > So, Foo::Foo (int) was implicitly used in the original program (?).
    > Where?
    >
    > If we are using 'operator*(int)' instead of 'operator*(int) const' in the
    > _original_ program a compiler has no problem too. Why?
    >
    >

    Because there would be no conversion in the operator*(int) anymore. Read my
    post if you haven't already.

    /Peter
    Peter Koch Larsen, Nov 26, 2004
    #5
    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. Gunnar

    int foo and int foo()

    Gunnar, Jan 19, 2004, in forum: C++
    Replies:
    8
    Views:
    379
    Robert Paul Clark
    Jan 22, 2004
  2. Tim Clacy
    Replies:
    15
    Views:
    2,681
    Kanenas
    May 30, 2005
  3. C. J. Clegg

    const int FOO vs #define FOO

    C. J. Clegg, Feb 20, 2006, in forum: C++
    Replies:
    9
    Views:
    369
    Jack Klein
    Feb 21, 2006
  4. Ralf Goertz

    const foo * and const foo &

    Ralf Goertz, Jan 26, 2011, in forum: C++
    Replies:
    3
    Views:
    522
    Ralf Goertz
    Jan 27, 2011
  5. TheFlyingDutchman
    Replies:
    5
    Views:
    1,825
    Johannes Schaub
    May 13, 2011
Loading...

Share This Page