 |
C++Talk.NET C++ language newsgroups
|
| View previous topic :: View next topic |
| Author |
Message |
catcher Guest
|
Posted: Tue Jun 24, 2003 7:34 pm Post subject: ambiguous or not? |
|
|
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?
|
|
| Back to top |
|
 |
Victor Bazarov Guest
|
Posted: Tue Jun 24, 2003 7:51 pm Post subject: Re: ambiguous or not? |
|
|
"catcher" <wangxin99 (AT) hotmail (DOT) com> wrote...
| Quote: | template<typename T
void fun(T x)
{};
|
Please lose the semicolon
| Quote: |
void fun(int x)
{};
|
Please lose the semicolon
| Quote: |
int main()
{
fun
}
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
|
|
| Back to top |
|
 |
catcher Guest
|
Posted: Thu Jun 26, 2003 2:50 pm Post subject: Re: ambiguous or not? |
|
|
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.
|
|
| Back to top |
|
 |
Victor Bazarov Guest
|
Posted: Thu Jun 26, 2003 3:41 pm Post subject: Re: ambiguous or not? |
|
|
"catcher" <wangxin99 (AT) hotmail (DOT) com> wrote...
| Quote: | 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
|
|
| Back to top |
|
 |
Powered by phpBB © 2001, 2006 phpBB Group
|