473,549 Members | 2,723 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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 2360

"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**********@Ho me.com
Jul 22 '05 #3

"Bob Hairgrove" <in*****@bigfoo t.com> wrote in message news:b1******** *************** *********@4ax.c om...
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*****@bigfoo t.com> wrote in message
news:b1******** *************** *********@4ax.c om...
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
4361
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
6920
by: vineoff | last post by:
struct C { operator const char*() { return "...."; } }; int main() { C a; 0; }
2
2882
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 <iostream> using namespace std;
0
1754
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 implementations of a member function of A that takes as an argument the abstract class Base (see Option 1 / Option 2): - Both compile without problems...
4
4846
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 _Predicate::argument_type argument_type; typedef typename _Predicate::return_type return_type; _Predicate pred_;
1
2103
by: developereo | last post by:
Hi folks, Can somebodyshed some light on this problem? class Interface { protected: Interface() { ...} virtual ~Interface() { ... } public:
5
2086
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, OutputIterator first, OutputIterator last) { while (first != last) *first++ = *source++;
0
7520
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main...
0
7957
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that...
1
7470
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For...
0
6043
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then...
0
5088
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert...
0
3500
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in...
0
3481
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
1941
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
1
1059
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.