473,320 Members | 1,887 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,320 software developers and data experts.

call of overloaded 'foo(short unsigned int*&)' is ambiguous

Could someone please tell me what is wrong with the following -ugly-
piece of c++ code. Why when I explicititely set the template parameter
my gcc compiler start getting confused:

bla.cxx: In function 'int main()':
bla.cxx:25: error: call of overloaded 'foo(short unsigned int*&)' is
ambiguous
bla.cxx:2: note: candidates are: void foo(OutputType*) [with PixelType
= short unsigned int, OutputType = short unsigned int]
bla.cxx:10: note: void foo(PixelType*) [with PixelType
= short unsigned int]

with code:
template <class PixelType,class OutputType>
void foo(OutputType *outputCurve)
{
PixelType pt;
}

template <class PixelType>
void foo(PixelType *outputCurve)
{
foo<PixelType,PixelType>(outputCurve);
}

int main()
{
unsigned short *o = 0;
// foo(o); // ok
foo<unsigned short>(o); // not ok
return 0;
}
Thanks !
Nov 17 '08 #1
3 6371
mathieu wrote:
Could someone please tell me what is wrong with the following -ugly-
piece of c++ code. Why when I explicititely set the template
parameter my gcc compiler start getting confused:

bla.cxx: In function 'int main()':
bla.cxx:25: error: call of overloaded 'foo(short unsigned int*&)' is
ambiguous
bla.cxx:2: note: candidates are: void foo(OutputType*) [with
PixelType = short unsigned int, OutputType = short unsigned int]
bla.cxx:10: note: void foo(PixelType*) [with
PixelType = short unsigned int]

with code:
template <class PixelType,class OutputType>
void foo(OutputType *outputCurve)
{
PixelType pt;
}

template <class PixelType>
void foo(PixelType *outputCurve)
{
foo<PixelType,PixelType>(outputCurve);
}

int main()
{
unsigned short *o = 0;
// foo(o); // ok
foo<unsigned short>(o); // not ok
return 0;
}

In the call to foo<unsigned short>(o), you explicitly say that
PixelType is unsigned short.

The compiler says - what if OutputType is also unsigned short?
Bo Persson


Nov 17 '08 #2
mathieu wrote:
Could someone please tell me what is wrong with the following -ugly-
piece of c++ code. Why when I explicititely set the template parameter
my gcc compiler start getting confused:

bla.cxx: In function 'int main()':
bla.cxx:25: error: call of overloaded 'foo(short unsigned int*&)' is
ambiguous
bla.cxx:2: note: candidates are: void foo(OutputType*) [with PixelType
= short unsigned int, OutputType = short unsigned int]
bla.cxx:10: note: void foo(PixelType*) [with PixelType
= short unsigned int]

with code:
template <class PixelType,class OutputType>
void foo(OutputType *outputCurve)
{
PixelType pt;
}

template <class PixelType>
void foo(PixelType *outputCurve)
{
foo<PixelType,PixelType>(outputCurve);
}

int main()
{
unsigned short *o = 0;
// foo(o); // ok
foo<unsigned short>(o); // not ok
return 0;
}
As you probably know, when you call a template function you are not
required to explicitly specify all template arguments. You can specify
none (in which case the compiler will try to deduce them), or you can
specify just a few of the leading arguments (in which case the compiler
will try to deduce the remaining ones).

In the first call

foo(o);

you don't specify any template arguments. The compiler considers both
versions of 'foo' template. In this case the compiler cannot use the
first version of 'foo' template as a candidate, because template
argument 'PixelType' is not deducible. The compiler is left with only
one candidate - the second version of 'foo' template - and successfully
uses it.

In the second call

foo<unsigned short>(o);

you specified one template argument. The compiler again considers both
versions of 'foo' template. This argument can be interpreted as the
first argument of the first 'foo' template (the one that was
non-deducible in the previous example). Since you specified it
explicitly, the compiler only has to deduce the second template
argument, which it can successfully do. So the first version becomes a
candidate in this case. The second version of 'foo' template is also a
candidate - with an explicitly specified argument. So now the compiler
has two candidates and both are equally good. Hence the ambiguity the
compiler is telling you about in its error messages.

However, it would be interesting to know whether C++ partial ordering
rules are supposed to resolve the ambiguity in this case. Is one of the
versions supposed to be recognized as "more specialized"? I'd say not,
based on what I see in C++98 specification. But Comeau Online compiler
seems to resolve the call in favor of the two-parameter version without
complaining about any ambiguities.

--
Best regards,
Andrey Tarasevich
Nov 17 '08 #3
On Nov 17, 6:33 pm, mathieu <mathieu.malate...@gmail.comwrote:
Could someone please tell me what is wrong with the following
-ugly- piece of c++ code. Why when I explicititely set the
template parameter my gcc compiler start getting confused:
bla.cxx: In function 'int main()':
bla.cxx:25: error: call of overloaded 'foo(short unsigned int*&)' is
ambiguous
bla.cxx:2: note: candidates are: void foo(OutputType*) [with PixelType
= short unsigned int, OutputType = short unsigned int]
bla.cxx:10: note: void foo(PixelType*) [with PixelType
= short unsigned int]
The compiler isn't confused; it's just doing what the standard
requires:-).
with code:
template <class PixelType,class OutputType>
void foo(OutputType *outputCurve)
{
PixelType pt;
}
template <class PixelType>
void foo(PixelType *outputCurve)
{
foo<PixelType,PixelType>(outputCurve);
}
int main()
{
unsigned short *o = 0;
// foo(o); // ok
foo<unsigned short>(o); // not ok
return 0;
}
OK. You have two function templates named foo, which will be
considered each time you invoke a function named foo; overload
resolution will determine which one is chosen. Strictly
speaking, function overload chooses between functions, not
between function templates; when you call a function for which
there are function templates, the compiler tries to deduce the
template arguments for each function template, and if it
succeeds, it adds the instantation (the instantiation of a
function template is a function) to the overload set.

In the first case, foo(o), template argument deduction fails for
the first function template; the compiler cannot deduce the type
of PixelType, so no function is added. It succeeds for the
second, with unsigned short for PixelType, the the function
foo<unsigned short>( unsigned short* ) is added to the overload
set. Since the overload set only contains a single function,
there is no ambiguity.

In the second case, where you call foo<unsigned short>, the
procedure is exactly the same. Except that argument deduction
works for both of the functions; for the first, it gives an
instantiation of foo<unsigned short, unsigned short>, and for
the second, an instantiation of foo<unsigned short>. (For the
second, there's really not much to deduce in the usual sense of
the word, but formally, deduction takes place, and the results
are added to the overload set.) The result is that you end up
with two functions with the same parameter, which results in an
ambiguity from overload resolution.

--
James Kanze (GABI Software) email:ja*********@gmail.com
Conseils en informatique orientée objet/
Beratung in objektorientierter Datenverarbeitung
9 place Sémard, 78210 St.-Cyr-l'École, France, +33 (0)1 30 23 00 34
Nov 18 '08 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

10
by: Jason Heyes | last post by:
I have a class with two constructors that both take the same type of argument as a parameter. What should I do to disambiguate my class?
3
by: CoolPint | last post by:
In the code below, shouldn't the function call minimum(a,a); result in compilation error? I read in Lippman's C++ Primer 3rd Edition on page 521, the call should be ambiguous. But on g++...
4
by: Alex Vinokur | last post by:
Why is it ambiguous? ------ foo.cpp ------ struct Foo { Foo operator* (Foo) { return Foo(); } Foo operator* (int) const { return Foo(); } Foo () {} Foo (int) {} };
3
by: Juha Nieminen | last post by:
Consider this code: void foo(int& i) { i += 10; } int main() { int a = 1;
10
by: Grizlyk | last post by:
1. Can abybody explain me why C++ function can not be overloaded by its return type? Return type can has priority higher than casting rules. For example: char input(); //can be compiled to name...
4
by: 9lives.9lives | last post by:
Hello, everyone! I am trying to optimize some code, but I don't think I'm doing what I think I'm doing. I profiled my code and found that the overloaded operator of my monomial class did...
8
by: RN1 | last post by:
The book I am referring to learn ASP states the following about the Int & Fix VBScript Maths functions: ========================================= Both Int & Fix return the integer portion of the...
3
by: valoh | last post by:
Hi, is this legal c++ code? template <typename BaseTstruct A { BaseT& this_() { return *static_cast<BaseT*>(this); } template <typename Tvoid Foo() { this_().Bar<T>(); } template...
3
by: bingfeng | last post by:
hello, anyone else can explain why following codes give wrong result while compiler accept it still? int & foo() {} int main() { int x = foo; std::cout << x << std::endl; }
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
1
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...

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.