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! ]
> 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.
This is a specialization for template parameters that are references.
There must also be a main template preceding this one.
Bo Persson
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.
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),