Re: Class template specialization with template parameter
Greg Herlihy wrote:
On May 14, 12:51 pm, flopbucket <flopbuc...@hotmail.com> wrote:
Hi,
First, thanks for the reply.
Foo<int> uses the normal template, but Foo<std::map<T1, T2> > for any
types of T1 and T2 uses the specifalization?
Not sure what you're asking here.
What I was trying to say was that if I declare:
Foo<int> myFoo();
I believe that you mean to declare an object (and not a function):
Foo<int> myFoo;
The instantiation will be for the normal/not-specialized template.
Yes.
But if I declare:
Foo<std::map<int, std::string> > myFoo2();
Again, I believe that you want:
Foo<std::map<int, std::string> > myFoo2;
it will use the specialization... and that regardless of the types
used for std::map (in this case, int and std::string), any
"Foo<std::map<typeA, typeB> > myFooExample()" will always
instantiate the specialization.
The answer depends on how many template parameters "std::map"
requires. The usual number is two - the two specified by the C++
Standard. An implementation however is allowed to require additional
temmplate type parameters for a std::map. So, assuming that std::map
requires only two type parameters, then the answer is "yes".
I don't think it has anything to do with the number of arguments the
template takes/has. The specialisation is not a template with a
template template argument (I believe that's what you're thinking of...)
For instance,
template<class T, class U = int> class TwoArgs {};
template<class T> struct Foo { enum {a=0}; };
template<class T> struct Foo<TwoArgs<T> > { enum {a=1}; };
int main() {
char should_complain[Foo<char>::a];
char dont_know[Foo<TwoArgs<int,char> >::a];
char should_be_OK[Foo<TwoArgs<double> >::a];
}
The declaration of 'should_complain' instantiates the regular 'Foo'
because it's not the specialisation on 'TwoArgs'. The declaration of
'should_be_OK' is the specialisation which has 'a == 1'. Now, since
'dont_know' uses the 'Foo' instantiated for 'TwoArgs', yet the second
argument of 'TwoArgs' is not the default (int), I am not sure. The
Comeau online test drive does not like 'dont_know', most likely because
it uses the 'TwoArgs' in a way different from the specialisation's, and
as the result 'a' is 0. Tricky...
V
--
Please remove capital 'A's when replying by e-mail
I do not respond to top-posted replies, please don't ask