Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > operator*(Foo) and operator*(int) const: ISO C++ says that these are ambiguous:

Reply
Thread Tools

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

 
 
Alex Vinokur
Guest
Posts: n/a
 
      11-26-2004
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:perator*(int) const
foo.cpp:3: note: candidate 2: Foo Foo: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


 
Reply With Quote
 
 
 
 
Peter Koch Larsen
Guest
Posts: n/a
 
      11-26-2004

"Alex Vinokur" <(E-Mail Removed)> skrev i en meddelelse
news:(E-Mail Removed)...
> 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:perator*(int) const
> foo.cpp:3: note: candidate 2: Foo Foo: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
>
>



 
Reply With Quote
 
 
 
 
Bob Hairgrove
Guest
Posts: n/a
 
      11-26-2004
On Fri, 26 Nov 2004 15:15:13 +0200, "Alex Vinokur"
<(E-Mail Removed)> 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:perator*(int) const
>foo.cpp:3: note: candidate 2: Foo Foo: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
http://www.velocityreviews.com/forums/(E-Mail Removed)
 
Reply With Quote
 
Alex Vinokur
Guest
Posts: n/a
 
      11-26-2004

"Bob Hairgrove" <(E-Mail Removed)> wrote in message news:(E-Mail Removed)...
> On Fri, 26 Nov 2004 15:15:13 +0200, "Alex Vinokur"
> <(E-Mail Removed)> 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:perator*(int) const
> >foo.cpp:3: note: candidate 2: Foo Foo: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









 
Reply With Quote
 
Peter Koch Larsen
Guest
Posts: n/a
 
      11-26-2004

"Alex Vinokur" <(E-Mail Removed)> skrev i en meddelelse
news:(E-Mail Removed)...
>
> "Bob Hairgrove" <(E-Mail Removed)> wrote in message
> news:(E-Mail Removed)...
>> On Fri, 26 Nov 2004 15:15:13 +0200, "Alex Vinokur"
>> <(E-Mail Removed)> 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:perator*(int) const
>> >foo.cpp:3: note: candidate 2: Foo Foo: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


 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
problem in running a basic code in python 3.3.0 that includes HTML file Satabdi Mukherjee Python 1 04-04-2013 07:48 PM
warning: ISO C++ says that these are ambiguous TheFlyingDutchman C++ 5 05-13-2011 11:59 AM
Dell says no & Acronis says maybe sysprep utility ( Re: Anyone use Acronis Drive Image 7.0? Bobby Fischler Computer Support 0 07-24-2004 12:12 AM



Advertisments