Topic: Ordinary function vs function template specialization


Author: fwai@arm.com (Francis Wai)
Date: 1997/11/07
Raw View
Valentin Bonnard <bonnardv@pratique.fr> writes:

>Steve Clamage wrote:
> >
> > On 03 Nov 97 07:29:31 GMT, Leif Rilbe <leif_rilbe@hotmail.com> wrote:

> > >Given a function template:
> > >       template <class T> T min(T,T);
> > >then,
> > >       template<> char* min<char*>(char*,char*);
> > >and,
> > >       char* min(char*,char*);
> > >
> > >are two different things.

> > Suppose a declaration of non-template min is in scope and you write
> >         min("hello", "world")
> > The non-template function is preferred over a template specialization,
> > and will be called. This example will not cause an instantiation of
> > min from the template.

>I don't think so. "hello" has type const char*, and a new
>specialisation is instantiated.

Err ..., "hello" used to have type "array of n char" but otherwise agrees.

--Francis Wai
---
[ 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         ]
[ FAQ:      http://reality.sgi.com/employees/austern_mti/std-c++/faq.html    ]
[ Policy:   http://reality.sgi.com/employees/austern_mti/std-c++/policy.html ]
[ Comments? mailto:std-c++-request@ncar.ucar.edu                             ]





Author: fjh@mundook.cs.mu.OZ.AU (Fergus Henderson)
Date: 1997/11/07
Raw View
fwai@arm.com (Francis Wai) writes:

>Valentin Bonnard <bonnardv@pratique.fr> writes:
>
>>I don't think so. "hello" has type const char*, and a new
>>specialisation is instantiated.
>
>Err ..., "hello" used to have type "array of n char" but otherwise agrees.

Neither of the above is quite correct.  In old versions of the draft,
"hello" used to have type "array of n char", but now it has type
"array of n const char".  When used as a template argument, template
argument deduction will use an template instantiation of type
`const char *', though (see 14.8.2[temp.deduct]/3).

--
Fergus Henderson <fjh@cs.mu.oz.au>   |  "I have always known that the pursuit
WWW: <http://www.cs.mu.oz.au/~fjh>   |  of excellence is a lethal habit"
PGP: finger fjh@128.250.37.3         |     -- the last words of T. S. Garp.
---
[ 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         ]
[ FAQ:      http://reality.sgi.com/employees/austern_mti/std-c++/faq.html    ]
[ Policy:   http://reality.sgi.com/employees/austern_mti/std-c++/policy.html ]
[ Comments? mailto:std-c++-request@ncar.ucar.edu                             ]





Author: stephen.clamage_nospam@eng.sun.com (Steve Clamage)
Date: 1997/11/07
Raw View
On 04 Nov 97 15:42:59 GMT, Valentin Bonnard <bonnardv@pratique.fr>
wrote:

 >Steve Clamage wrote:
 > >
 > > On 03 Nov 97 07:29:31 GMT, Leif Rilbe <leif_rilbe@hotmail.com> wrote:
 >
 > > >Given a function template:
 > > >       template <class T> T min(T,T);
 > > >then,
 > > >       template<> char* min<char*>(char*,char*);
 > > >and,
 > > >       char* min(char*,char*);
 > > >
 > > >are two different things.
 >
 > > Suppose a declaration of non-template min is in scope and you write
 > >         min("hello", "world")
 > > The non-template function is preferred over a template specialization,
 > > and will be called. This example will not cause an instantiation of
 > > min from the template.
 >
 >I don't think so. "hello" has type const char*, and a new
 >specialisation is instantiated.

Yes. I picked a bad example.

---
Steve Clamage, stephen.clamage_nospam@eng.sun.com
( Note: remove "_nospam" when replying )
---
[ 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         ]
[ FAQ:      http://reality.sgi.com/employees/austern_mti/std-c++/faq.html    ]
[ Policy:   http://reality.sgi.com/employees/austern_mti/std-c++/policy.html ]
[ Comments? mailto:std-c++-request@ncar.ucar.edu                             ]





Author: Valentin Bonnard <bonnardv@pratique.fr>
Date: 1997/11/04
Raw View
Steve Clamage wrote:
 >
 > On 03 Nov 97 07:29:31 GMT, Leif Rilbe <leif_rilbe@hotmail.com> wrote:

 > >Given a function template:
 > >       template <class T> T min(T,T);
 > >then,
 > >       template<> char* min<char*>(char*,char*);
 > >and,
 > >       char* min(char*,char*);
 > >
 > >are two different things.

 > Suppose a declaration of non-template min is in scope and you write
 >         min("hello", "world")
 > The non-template function is preferred over a template specialization,
 > and will be called. This example will not cause an instantiation of
 > min from the template.

I don't think so. "hello" has type const char*, and a new
specialisation is instantiated.

--

Valentin Bonnard                mailto:bonnardv@pratique.fr
info about C++/a propos du C++: http://www.pratique.fr/~bonnardv/
---
[ 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         ]
[ FAQ:      http://reality.sgi.com/employees/austern_mti/std-c++/faq.html    ]
[ Policy:   http://reality.sgi.com/employees/austern_mti/std-c++/policy.html ]
[ Comments? mailto:std-c++-request@ncar.ucar.edu                             ]





Author: Leif Rilbe <leif_rilbe@hotmail.com>
Date: 1997/11/03
Raw View
Reading the Dec. 1996 draft I get a bit confused regarding the
difference between an ordinary function and a function template
specialization. In =A710 of section 14.7.3 it is stated: "A function with
the same name as a template and a type that exactly matches that of a
template specialization is not an explicit  specialization".

I interpret this as follows.

Given a function template:

 template <class T> T min(T,T);

then,

 template<> char* min<char*>(char*,char*);

and,

 char* min(char*,char*);

are two different things. Is this a correct interpretation? If so, what
is the difference? In MS Visual C++ v5.0 i can compile, link, and
execute code that use the two notations interchangeably. I can use one
for the declaration and the other for the definition and it still works.
Is this a bug in the MS compiler? What is the difference supposed to
consist of? Where is it described?

Regards,
  Leif
---
[ 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         ]
[ FAQ:      http://reality.sgi.com/employees/austern_mti/std-c++/faq.html    ]
[ Policy:   http://reality.sgi.com/employees/austern_mti/std-c++/policy.html ]
[ Comments? mailto:std-c++-request@ncar.ucar.edu                             ]





Author: stephen.clamage_nospam@eng.sun.com (Steve Clamage)
Date: 1997/11/04
Raw View
On 03 Nov 97 07:29:31 GMT, Leif Rilbe <leif_rilbe@hotmail.com> wrote:

>Reading the Dec. 1996 draft I get a bit confused regarding the
>difference between an ordinary function and a function template
>specialization. In =A710 of section 14.7.3 it is stated: "A function wit=
h
>the same name as a template and a type that exactly matches that of a
>template specialization is not an explicit  specialization".
>
>I interpret this as follows.
>Given a function template:
> template <class T> T min(T,T);
>then,
> template<> char* min<char*>(char*,char*);
>and,
> char* min(char*,char*);
>
>are two different things. Is this a correct interpretation? If so, what
>is the difference?

Yes, that is correct. The difference is that the presence of the
non-template min does not necessarily prevent the generation of a
specialization.

Suppose a declaration of non-template min is in scope and you write
 min("hello", "world")
The non-template function is preferred over a template specialization,
and will be called. This example will not cause an instantiation of
min from the template.

But if a declaration of non-template min is not in scope, a
sepcialization of template min will be generated. The two functions
have different names and can coexist in one program.

---
Steve Clamage, stephen.clamage_nospam@eng.sun.com
( Note: remove "_nospam" when replying )
---
[ 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         ]
[ FAQ:      http://reality.sgi.com/employees/austern_mti/std-c++/faq.html    ]
[ Policy:   http://reality.sgi.com/employees/austern_mti/std-c++/policy.html ]
[ Comments? mailto:std-c++-request@ncar.ucar.edu                             ]