472,139 Members | 1,487 Online
Bytes | Software Development & Data Engineering Community
Post +

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 472,139 software developers and data experts.

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

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::operator*(int) const
foo.cpp:3: note: candidate 2: Foo Foo::operator*(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
Jul 22 '05 #1
4 2238

"Alex Vinokur" <al****@big-foot.com> skrev i en meddelelse
news:30*************@uni-berlin.de...
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::operator*(int) const
foo.cpp:3: note: candidate 2: Foo Foo::operator*(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

Jul 22 '05 #2
On Fri, 26 Nov 2004 15:15:13 +0200, "Alex Vinokur"
<al****@big-foot.com> 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::operator*(int) const
foo.cpp:3: note: candidate 2: Foo Foo::operator*(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
No**********@Home.com
Jul 22 '05 #3

"Bob Hairgrove" <in*****@bigfoot.com> wrote in message news:b1********************************@4ax.com...
On Fri, 26 Nov 2004 15:15:13 +0200, "Alex Vinokur"
<al****@big-foot.com> 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::operator*(int) const
foo.cpp:3: note: candidate 2: Foo Foo::operator*(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



Jul 22 '05 #4

"Alex Vinokur" <al****@big-foot.com> skrev i en meddelelse
news:30*************@uni-berlin.de...

"Bob Hairgrove" <in*****@bigfoot.com> wrote in message
news:b1********************************@4ax.com...
On Fri, 26 Nov 2004 15:15:13 +0200, "Alex Vinokur"
<al****@big-foot.com> 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::operator*(int) const
>foo.cpp:3: note: candidate 2: Foo Foo::operator*(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
Jul 22 '05 #5

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

11 posts views Thread by vineoff | last post: by
5 posts views Thread by Chris Forone | last post: by

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.