473,396 Members | 1,996 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 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 2345

"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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

15
by: Tim Clacy | last post by:
Please illuminate; what operator of class 'Event' will get matched for these two cases : Event ev1; Event ev2; // Case 1 // if (ev1) ;
11
by: vineoff | last post by:
struct C { operator const char*() { return "...."; } }; int main() { C a; 0; }
2
by: Claudius | last post by:
Hello, I have written a class A with the access operator(int) overloaded by a A-const version which returns an int by value: ------------------------------------------------ #include...
0
by: Pedro | last post by:
Hello pythonians! ;-D , I have a little problem when I expose (assisted by boost.python) classes with virtual functions, specially with operator(). In the C++ code below I test two different...
4
by: Lycan. Mao.. | last post by:
Hello, I'm trying to write a function adapter object, but it fails with the above information. Can you help me. template <typename _Predicate> struct Unary_negate { typedef typename...
1
by: developereo | last post by:
Hi folks, Can somebodyshed some light on this problem? class Interface { protected: Interface() { ...} virtual ~Interface() { ... } public:
5
by: Chris Forone | last post by:
hello group, why i cant make the InputIterator of the following func-temp const? template<typename InputIterator, typename OutputIterator> OutputIterator Types::Copy(InputIterator source,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

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.