473,789 Members | 2,408 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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,P ixelType>(outpu tCurve);
}

int main()
{
unsigned short *o = 0;
// foo(o); // ok
foo<unsigned short>(o); // not ok
return 0;
}
Thanks !
Nov 17 '08 #1
3 6407
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,P ixelType>(outpu tCurve);
}

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,P ixelType>(outpu tCurve);
}

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.comwr ote:
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,P ixelType>(outpu tCurve);
}
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 objektorientier ter Datenverarbeitu ng
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
6141
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
1448
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++ (versions 3.2.3 and 3.4.2) it compiles fine and the result shows that minimum(a,a) is resolved using minimum(T,T) I also tried it on http://www.comeaucomputing.com/tryitout/ and got the same result.
4
2380
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
2271
by: Juha Nieminen | last post by:
Consider this code: void foo(int& i) { i += 10; } int main() { int a = 1;
10
2629
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 "input$char$void" int input(); //can be compiled to name "input$int$void" .... int i= 3+'0'+input(); //can be compiled to: int(3)+int('0')+input$int$void()
4
4726
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 6384690328 calls in 33.67 seconds. 33.67 6384690328 Monomial::operator(int) const Since the parameter was an int, I made it const int& since I reasoned I would be saving a copy of the int parameter to the stack by changing from a pass-by-value to...
8
2979
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 number but the difference lies in handling negative numbers. Int returns the first integer lesser than or equal to the number whereas Fix returns the first integer greater than or equal to the number. =========================================
3
1615
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 <typename Tvoid Bar() { }
3
2584
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
9663
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 usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9506
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10404
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
1
10136
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 most users, this new feature is actually very convenient. If you want to control the update process,...
0
9979
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
9016
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 launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
6761
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 into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5548
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
2
3695
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.