Topic: template keyword and member template specializations


Author: Richard Corden <richards_corden@hotmail.com>
Date: Fri, 19 Oct 2001 15:16:37 GMT
Raw View
Hi,

I recently came across the following code:

struct A
{
  void foo (int);
  template <typename T>
  void foo (T);
};

void bar (A & a)
{
  a.template foo(0); // Can we use template keyword here?
}

and I was unsure if the template keyword was valid when
the name is not a template-id.

Is 14.2 describing template-ids, and so not refering to
template-names without explicit arguments.
This would mean that the template keyword is really just
a hint for parsing?

This would be consistent with the note in 14.8.1;2 which
says that an empty template argument list can be used to
indicate that a given use refers to a specialization of
a function template even when a normal (eg. non-template)
function is visible that would otherwise be used.

I know that several compilers, including Comeau in strict
mode, accept this construct.

Thanks for your time,

Richard

--
Richard Corden
To reply remove 's' from address

---
[ 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, 22 Oct 2001 15:44:19 GMT
Raw View
"Richard Corden" <richards_corden@hotmail.com> wrote in message
news:83snchggqm.fsf@leinster.programmingresearch.ie...
>
> Hi,
>
> I recently came across the following code:
>
> struct A
> {
>   void foo (int);
>   template <typename T>
>   void foo (T);
> };
>
> void bar (A & a)
> {
>   a.template foo(0); // Can we use template keyword here?
> }
>
> and I was unsure if the template keyword was valid when
> the name is not a template-id.

This is valid. It tells the compiler to use the template member function
foo, rather than the non-template version. It is an error to use the keyword
"template" if the named member is not a template. If you wish to name a
specialization of a member template with a template-id, it is an error NOT
to use "template" - e.g.

a.foo<int>(0); // error
a.template foo<int>(0); //OK
a.foo(0); // non-template foo
a.template foo(0); // foo<int>
a.template foo(3.5); // foo<double>

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                ]