Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > ambiguous or not?

Reply
Thread Tools

ambiguous or not?

 
 
catcher
Guest
Posts: n/a
 
      06-24-2003
template<typename T>
void fun(T x)
{};

void fun(int x)
{};

int main()
{
fun<int>(3);
}

This code is ambiguous or not?
What the standard says about it?
 
Reply With Quote
 
 
 
 
Victor Bazarov
Guest
Posts: n/a
 
      06-24-2003
"catcher" <(E-Mail Removed)> wrote...
> template<typename T>
> void fun(T x)
> {};


Please lose the semicolon

>
> void fun(int x)
> {};


Please lose the semicolon

>
> int main()
> {
> fun<int>(3);
> }
>
> This code is ambiguous or not?
> What the standard says about it?


No, it is not ambiguous. The template function instantiated
for 'T == int' should be used. And even if you drop the <int>
thing after the name, it's still unambiguous -- the regular
function should be used. See Clause 14, subs 14.2 and 14.8.3
along with 13.3.3...

Victor


 
Reply With Quote
 
 
 
 
catcher
Guest
Posts: n/a
 
      06-26-2003
I checked the standard, It says that the common function has higher
priority than template function, when ambiguity happens. so fun(3)
will call a common function.
But I cant understand why fun<int>(3) should call a template function.
I cant find the item about it in the standard. In fact, I compiled
these codes using Intel C++, and the result is insteresting:
fun<int>(3) call a common function, not a template function.
 
Reply With Quote
 
Victor Bazarov
Guest
Posts: n/a
 
      06-26-2003
"catcher" <(E-Mail Removed)> wrote...
> I checked the standard, It says that the common function has higher
> priority than template function, when ambiguity happens. so fun(3)
> will call a common function.
> But I cant understand why fun<int>(3) should call a template function.
> I cant find the item about it in the standard. In fact, I compiled
> these codes using Intel C++, and the result is insteresting:
> fun<int>(3) call a common function, not a template function.


It is quite possible that I'm not reading it correctly. Asking
in comp.std.c++ is advised. I understand it that if the template
argument list is specified, then non-template functions are not
added to the set of candidate functions. However, 14.8.3/1 has
the following sentence: "The complete set of candidate functions
includes all the function templates instantiated in this way and
all of the non-template overloaded functions of the same name"
which may be interpreted that only the NAME ('fun' in your case)
is going to be considered for identification purposes.

However, after further search I found 14.3/7, which says, "When
the template in a template-id is an overloaded function template,
both non-template functions in the overload set and function
templates in the overload set for which the template-arguments do
not match the template-parameters are ignored. If none of the
function templates have matching template-parameters, the program
is ill-formed." [To help understand, "template-id" is formed by
an identifier followed by an optionally empty set of angle brackets,
as in "fun<int>"]

That seems pretty straight-forward to me. 'fun<int>' should force
the compiler to ignore 'fun' or 'fun<double>' or whatever else is
out there.

Victor


 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Ambiguous reference to type jahaya@gmail.com VHDL 2 09-12-2005 09:55 AM
'CrystalReportViewer' is ambiguous in the namespace 'CrystalDecisions. Nancy. ASP .Net 3 09-28-2004 08:21 PM
70-300, ambiguous question Session MCSD 2 05-04-2004 05:55 PM
Ambiguous type? Tuukka Toivonen VHDL 3 05-03-2004 02:36 PM
Parser Error Message: Ambiguous match found. Denon ASP .Net 2 01-02-2004 09:19 AM



Advertisments