Re: Class template specialization with template parameter
On May 14, 10:20 pm, Greg Herlihy <gre...@mac.com> wrote:
On May 14, 12:51 pm, flopbucket <flopbuc...@hotmail.com> wrote:
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.
The usual number is four, since that's what the C++ standard
requires. He has defined a partial specialization for Foo<
std::map< T, U, std::less<T>, std::allocator< std::pair< T
const, U > >. If he instantiates Foo with a type which
corresponds to this, the partial specialization will be used.
Otherwise, the non-specialized version will be used. Thus, with
std::map< int, std::string > (or std::map< int, std::string,
std::less< int > >), he'll get the partial specialization, but
with std::map< std::string, int, CaseInsensitiveCompare >, he'll
get the non-specialized version.
An implementation however is allowed to require additional
temmplate type parameters for a std::map.
But only if they follow the required four, and have default
values. So as long as you don't use them, his code should work.
So, assuming that std::map requires only two type parameters,
then the answer is "yes".
Assuming that the implementation of std::map is conform, his
partial specialization will be used for all std::map
instantiations which use std::less<Key> as the comparator, and
std::allocator< std::pair< Key const, T > > as the allocator.
(Using something other than the default allocator is fairly
rare, but it's quite frequent to find map's with other than the
default comparison function. In his case, I would definitly add
that to the arguments of the partial specialization. And it's
not that difficult to add the allocator either:
template< typename Key, typename Value,
typename Cmp, typename Alloc >
class Foo< std::map< Key, Value, Cmp, Alloc > >
{
// ...
} ;
This should work for all instanciations of std::map.
--
James Kanze (GABI Software) email:james.kanze@gmail.com
Conseils en informatique orient=E9e objet/
Beratung in objektorientierter Datenverarbeitung
9 place S=E9mard, 78210 St.-Cyr-l'=C9cole, France, +33 (0)1 30 23 00 34