Topic: Template functions
Author: "Anthony Williams" <anthwil@nortelnetworks.com>
Date: Tue, 25 Sep 2001 15:34:09 GMT Raw View
"Gabriel Dos Reis" <dosreis@cmla.ens-cachan.fr> wrote in message
news:flzo7ks4sl.fsf@jambon.cmla.ens-cachan.fr...
> "Gregg" <Gregg47@jjcomp.com> writes:
>
> | Anthony Williams wrote in message
> | <9omr00$do22g$1@ID-49767.news.dfncis.de>...
> | >"Gregg" <Gregg47@jjcomp.com> wrote in message
> | >news:LCor7.30179$QK.24052402@news1.sttln1.wa.home.com...
> | >> Given a template:
> | >>
> | >> template<class T> int foo(T t) { ... }
> | >>
> | >> What is the difference between a non-template function:
> | >>
> | >> int foo(double) { ... }
> | >##1
> | >>
> | >> and an explicit specialization:
> | >>
> | >> template<> int foo<double>(double t) { ... }
> | >##2
> | >>
> | >> of functions with the same name?
> | >
> | >They are distinct overloads of the same function name, that happen to
have
> | >the same signature.
> |
> |
> | Wouldn't that cause a collision of the two functions at compile time, or
at
> | least at link time?
The names must be mangled differently - one is a template specialization,
the other is not.
> No, because the two functions do -not- have the same name contrary to
> what was said.
One is a specialization of a function template named foo, the other is a
function named foo. Individually, they can both be called as
foo(1.3);
14.5.5p2 says:
"A normal function is not related to a function template (i.e., it is never
considered to be a specialization), even if it has the same name and type as
a potentially generated function template specialization."
This is the case here - a normal function with the same name and type as a
function template specialization.
Anthony
--
Anthony Williams
Software Engineer, Nortel Networks Optoelectronics
The opinions expressed in this message are not necessarily those of my
employer
---
[ comp.std.c++ is moderated. To submit articles, try just posting with ]
[ your news-reader. If that fails, use mailto:std-c++@ncar.ucar.edu ]
[ --- Please see the FAQ before posting. --- ]
[ FAQ: http://www.research.att.com/~austern/csc/faq.html ]
Author: Gabriel Dos Reis <dosreis@cmla.ens-cachan.fr>
Date: Tue, 25 Sep 2001 23:56:25 GMT Raw View
"Anthony Williams" <anthwil@nortelnetworks.com> writes:
| > | Wouldn't that cause a collision of the two functions at compile time, or
| at
| > | least at link time?
|
| The names must be mangled differently - one is a template specialization,
| the other is not.
The C++ definition does not speak of "mangling". Futermore, even
without the explicite specialization, if the implicit specialization
were to have the same name as the non-template function with the same
signature, then there would be a problem with the ODR.
| > No, because the two functions do -not- have the same name contrary to
| > what was said.
|
| One is a specialization of a function template named foo, the other is a
| function named foo.
The function *template* name is "foo", but that of the specilization
(for double) is "foo<double>" -- i.e a template-id. See
14.2 "Names of template specialization".
| Individually, they can both be called as
|
| foo(1.3);
That doesn't imply that the specialization's name is "foo". "foo" is
the name of the template, not that of the specialization.
--
Gabriel Dos Reis, dosreis@cmla.ens-cachan.fr
---
[ comp.std.c++ is moderated. To submit articles, try just posting with ]
[ your news-reader. If that fails, use mailto:std-c++@ncar.ucar.edu ]
[ --- Please see the FAQ before posting. --- ]
[ FAQ: http://www.research.att.com/~austern/csc/faq.html ]
Author: "Gregg" <Gregg47@jjcomp.com>
Date: Sun, 23 Sep 2001 18:33:51 GMT Raw View
Given a template:
template<class T> int foo(T t) { ... }
What is the difference between a non-template function:
int foo(double) { ... }
and an explicit specialization:
template<> int foo<double>(double t) { ... }
of functions with the same name?
---
[ comp.std.c++ is moderated. To submit articles, try just posting with ]
[ your news-reader. If that fails, use mailto:std-c++@ncar.ucar.edu ]
[ --- Please see the FAQ before posting. --- ]
[ FAQ: http://www.research.att.com/~austern/csc/faq.html ]
Author: Francis Glassborow <francis.glassborow@ntlworld.com>
Date: Mon, 24 Sep 2001 15:27:41 GMT Raw View
In article <LCor7.30179$QK.24052402@news1.sttln1.wa.home.com>, Gregg
<Gregg47@jjcomp.com> writes
>Given a template:
>
> template<class T> int foo(T t) { ... }
>
>What is the difference between a non-template function:
>
> int foo(double) { ... }
>
>and an explicit specialization:
>
> template<> int foo<double>(double t) { ... }
>
>of functions with the same name?
Possibly friendship. If the template is a friend so is the
specialisation but not the plain function.
Francis Glassborow
I offer my sympathy and prayers to all those who are suffering
as a result of the events of September 11 2001.
---
[ comp.std.c++ is moderated. To submit articles, try just posting with ]
[ your news-reader. If that fails, use mailto:std-c++@ncar.ucar.edu ]
[ --- Please see the FAQ before posting. --- ]
[ FAQ: http://www.research.att.com/~austern/csc/faq.html ]
Author: "Anthony Williams" <anthwil@nortelnetworks.com>
Date: Mon, 24 Sep 2001 15:27:53 GMT Raw View
"Gregg" <Gregg47@jjcomp.com> wrote in message
news:LCor7.30179$QK.24052402@news1.sttln1.wa.home.com...
> Given a template:
>
> template<class T> int foo(T t) { ... }
>
> What is the difference between a non-template function:
>
> int foo(double) { ... }
##1
>
> and an explicit specialization:
>
> template<> int foo<double>(double t) { ... }
##2
>
> of functions with the same name?
They are distinct overloads of the same function name, that happen to have
the same signature.
The template form ##2 can be accessed using the foo<double>(3.1) syntax,
whereas the non-template form ##1 cannot.
The non-template form will accept implicit conversions, whereas the template
form does not.
The non-template form is preferred to the template form where the function
is named just as "foo", rather than foo<double>, and the given argument is
of type double.
foo(3.1); // call ##1
foo(4); // call foo<int> - int->int identity is better than int->double
promotion.
foo<double>(4); // call ##2
Anthony
--
Anthony Williams
Software Engineer, Nortel Networks Optoelectronics
The opinions expressed in this message are not necessarily those of my
employer
---
[ comp.std.c++ is moderated. To submit articles, try just posting with ]
[ your news-reader. If that fails, use mailto:std-c++@ncar.ucar.edu ]
[ --- Please see the FAQ before posting. --- ]
[ FAQ: http://www.research.att.com/~austern/csc/faq.html ]
Author: Gabriel Dos Reis <dosreis@cmla.ens-cachan.fr>
Date: Mon, 24 Sep 2001 20:13:02 GMT Raw View
"Anthony Williams" <anthwil@nortelnetworks.com> writes:
| "Gregg" <Gregg47@jjcomp.com> wrote in message
| news:LCor7.30179$QK.24052402@news1.sttln1.wa.home.com...
| > Given a template:
| >
| > template<class T> int foo(T t) { ... }
| >
| > What is the difference between a non-template function:
| >
| > int foo(double) { ... }
| ##1
| >
| > and an explicit specialization:
| >
| > template<> int foo<double>(double t) { ... }
| ##2
| >
| > of functions with the same name?
|
| They are distinct overloads of the same function name, that happen to have
| the same signature.
>From a technical point of view, that is incorrect. The name of the
function defined in
template<> int foo<double>(double t) { ... }
is "foo<double>", not "foo".
--
Gabriel Dos Reis, dosreis@cmla.ens-cachan.fr
---
[ comp.std.c++ is moderated. To submit articles, try just posting with ]
[ your news-reader. If that fails, use mailto:std-c++@ncar.ucar.edu ]
[ --- Please see the FAQ before posting. --- ]
[ FAQ: http://www.research.att.com/~austern/csc/faq.html ]
Author: "Gregg" <Gregg47@jjcomp.com>
Date: Mon, 24 Sep 2001 20:13:05 GMT Raw View
Anthony Williams wrote in message
<9omr00$do22g$1@ID-49767.news.dfncis.de>...
>"Gregg" <Gregg47@jjcomp.com> wrote in message
>news:LCor7.30179$QK.24052402@news1.sttln1.wa.home.com...
>> Given a template:
>>
>> template<class T> int foo(T t) { ... }
>>
>> What is the difference between a non-template function:
>>
>> int foo(double) { ... }
>##1
>>
>> and an explicit specialization:
>>
>> template<> int foo<double>(double t) { ... }
>##2
>>
>> of functions with the same name?
>
>They are distinct overloads of the same function name, that happen to have
>the same signature.
Wouldn't that cause a collision of the two functions at compile time, or at
least at link time?
>The template form ##2 can be accessed using the foo<double>(3.1) syntax,
>whereas the non-template form ##1 cannot.
>The non-template form will accept implicit conversions, whereas the
template
>form does not.
>
>The non-template form is preferred to the template form where the function
>is named just as "foo", rather than foo<double>, and the given argument is
>of type double.
>
>foo(3.1); // call ##1
>foo(4); // call foo<int> - int->int identity is better than int->double
>promotion.
>foo<double>(4); // call ##2
>
>Anthony
---
[ comp.std.c++ is moderated. To submit articles, try just posting with ]
[ your news-reader. If that fails, use mailto:std-c++@ncar.ucar.edu ]
[ --- Please see the FAQ before posting. --- ]
[ FAQ: http://www.research.att.com/~austern/csc/faq.html ]
Author: Gabriel Dos Reis <dosreis@cmla.ens-cachan.fr>
Date: Mon, 24 Sep 2001 20:40:26 GMT Raw View
"Gregg" <Gregg47@jjcomp.com> writes:
| Anthony Williams wrote in message
| <9omr00$do22g$1@ID-49767.news.dfncis.de>...
| >"Gregg" <Gregg47@jjcomp.com> wrote in message
| >news:LCor7.30179$QK.24052402@news1.sttln1.wa.home.com...
| >> Given a template:
| >>
| >> template<class T> int foo(T t) { ... }
| >>
| >> What is the difference between a non-template function:
| >>
| >> int foo(double) { ... }
| >##1
| >>
| >> and an explicit specialization:
| >>
| >> template<> int foo<double>(double t) { ... }
| >##2
| >>
| >> of functions with the same name?
| >
| >They are distinct overloads of the same function name, that happen to have
| >the same signature.
|
|
| Wouldn't that cause a collision of the two functions at compile time, or at
| least at link time?
No, because the two functions do -not- have the same name contrary to
what was said.
--
Gabriel Dos Reis, dosreis@cmla.ens-cachan.fr
---
[ comp.std.c++ is moderated. To submit articles, try just posting with ]
[ your news-reader. If that fails, use mailto:std-c++@ncar.ucar.edu ]
[ --- Please see the FAQ before posting. --- ]
[ FAQ: http://www.research.att.com/~austern/csc/faq.html ]