Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

syntax about function template, or not

1 view
Skip to first unread message

yuanfang

unread,
Nov 6, 2009, 9:03:03 PM11/6/09
to
Forgive my ignorance, I ran into this "little" function coming from
LLVM source base about so called "enhanced RTTI", still couldn't
figure out what does it mean.

1 // Define reference traits in terms of base traits...
2 template<class FromCl>
3 struct isa_impl_cl<FromCl&> {
4 template<class ToCl>
5 static bool isa(FromCl &Val) {
6 return isa_impl_cl<FromCl>::template isa<ToCl>(&Val);
7 }
8 };

especially, at line 3, how come there is another template-like thing.
Thank you guys who is willing to reply.

--
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]

Thomas Maeder

unread,
Nov 7, 2009, 10:19:30 AM11/7/09
to
yuanfang <tabloid...@gmail.com> writes:

> 1 // Define reference traits in terms of base traits...
> 2 template<class FromCl>
> 3 struct isa_impl_cl<FromCl&> {
> 4 template<class ToCl>
> 5 static bool isa(FromCl &Val) {
> 6 return isa_impl_cl<FromCl>::template isa<ToCl>(&Val);
> 7 }
> 8 };
>
> especially, at line 3, how come there is another template-like thing.

This is a partial specialization of a base template.

There has to be a base template isa_impl_cl somewhere higher up in the
code, along the lines of

template <class T>
struct isa_impl_cl
{
...
};


Imagine the base template as a generic version of what isa_impl_cl
should look like for most types T.

Now isa_impl_cl seems to have to be something different if it is
instantiated on a reference type. That's where the code you posted
comes in. If isa_impl_cl is instantiated on a reference type, the
compiler will use this more specialized version.

Bo Persson

unread,
Nov 7, 2009, 10:21:30 AM11/7/09
to
yuanfang wrote:
> Forgive my ignorance, I ran into this "little" function coming from
> LLVM source base about so called "enhanced RTTI", still couldn't
> figure out what does it mean.
>
> 1 // Define reference traits in terms of base traits...
> 2 template<class FromCl>
> 3 struct isa_impl_cl<FromCl&> {
> 4 template<class ToCl>
> 5 static bool isa(FromCl &Val) {
> 6 return isa_impl_cl<FromCl>::template isa<ToCl>(&Val);
> 7 }
> 8 };
>
> especially, at line 3, how come there is another template-like
> thing. Thank you guys who is willing to reply.

This is a specialization for template parameters that are references.

There must also be a main template preceding this one.

Bo Persson

tohava

unread,
Nov 7, 2009, 10:22:12 AM11/7/09
to
On Nov 7, 4:03 am, yuanfang <tabloid.adr...@gmail.com> wrote:
> Forgive my ignorance, I ran into this "little" function coming from
> LLVM source base about so called "enhanced RTTI", still couldn't
> figure out what does it mean.
>
> 1 // Define reference traits in terms of base traits...
> 2 template<class FromCl>
> 3 struct isa_impl_cl<FromCl&> {
> 4 template<class ToCl>
> 5 static bool isa(FromCl &Val) {
> 6 return isa_impl_cl<FromCl>::template isa<ToCl>(&Val);
> 7 }
> 8 };
>
> especially, at line 3, how come there is another template-like thing.
> Thank you guys who is willing to reply.
>

This function simply calls another version of this function that gets
a pointer instead of a reference. The thing in line 3 is a partial
specialization of the struct for templates only.

Jeff Schwab

unread,
Nov 7, 2009, 10:20:28 AM11/7/09
to
yuanfang wrote:
> Forgive my ignorance, I ran into this "little" function coming from
> LLVM source base about so called "enhanced RTTI", still couldn't
> figure out what does it mean.
>
> 1 // Define reference traits in terms of base traits...
> 2 template<class FromCl>
> 3 struct isa_impl_cl<FromCl&> {
> 4 template<class ToCl>
> 5 static bool isa(FromCl &Val) {
> 6 return isa_impl_cl<FromCl>::template isa<ToCl>(&Val);
> 7 }
> 8 };
>
> especially, at line 3, how come there is another template-like thing.
> Thank you guys who is willing to reply.

That's a partial specialization. There should already have been a
declaration (and probably a definition, called the "primary template")
template<class FromCl> struct isa_impl_cl. In the code you posted, the
author has effectively told the compiler "when you instantiate the
isa_impl_cl template, if the template argument is a reference type, use
this special definition instead of the original." In the body, the
"isa" member function template just delegates to a similarly named
member function template in the primary template definition. The point
apparently is that the original "isa" accepts a pointer, whereas this
specialization accepts a reference (and just passes the address of the
referenced object down to the original definition),

0 new messages