ambiguous or not?

C

catcher

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?
 
V

Victor Bazarov

catcher said:
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
 
C

catcher

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.
 
V

Victor Bazarov

catcher said:
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
 

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,755
Messages
2,569,536
Members
45,013
Latest member
KatriceSwa

Latest Threads

Top