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

A

Alex Vinokur

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

Peter Koch Larsen

Alex Vinokur said:
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)

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
 
B

Bob Hairgrove

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)

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

Alex Vinokur

Bob Hairgrove said:
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?
 
P

Peter Koch Larsen

Alex Vinokur said:
Bob Hairgrove said:
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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,995
Messages
2,570,228
Members
46,818
Latest member
SapanaCarpetStudio

Latest Threads

Top