I am pondering about partial specialization
. While I understand the idea, I haven't seen any real-world usage of this technique. Full specialization
is used in many places in STL
so I don't have a problem with that. Could you educate me about a real-world example where partial specialization
is used? If the example is in STL
that would be superior!

- 94,250
- 39
- 176
- 234
-
Sounds like homework ;P – Aiden Bell Aug 27 '09 at 16:48
-
@Aiden really?! I don't think they allow these things in college! – Khaled Alshaya Aug 27 '09 at 16:51
-
@Arak ... I forgot collage is all Java and MSWord .. sorry bout that ;) – Aiden Bell Aug 27 '09 at 16:54
-
@Aiden, now you are saying the truth :D – Khaled Alshaya Aug 27 '09 at 16:56
-
No one taught us MS Word in college. :( – jalf Aug 27 '09 at 18:32
-
@jalf, be glad. our teacher told us how to maximize a MS Word window using the mouse. – Johannes Schaub - litb Aug 27 '09 at 19:05
3 Answers
C++0x comes with unique_ptr
which is a replacement for auto_ptr
which is going to be deprecated.
If you use unique_ptr
with an array type, it uses delete[]
to free it, and to provide operator[]
etc. If you use it with a non-array type, it uses delete
. This needs partial template specialization like
template<typename T>
struct my_unique_ptr { ... };
template<typename T>
struct my_unique_ptr<T[]> { ... };
Another use (although a very questionable) is std::vector<bool, Allocator>
in the standard library. The bool specialization uses a space optimization to pack bools into individual bits
template<typename T, typename Allocator = std::allocator<T> >
struct vector { ... };
template<typename Allocator>
struct vector<bool, Allocator> { ... };
Yet another use is with std::iterator_traits<T>
. Iterators are required to define the nested typedefs value_type
, reference
and others to the correct types (for a const iterator, reference
would usually be T const&
, for example) so algorithms may use them for their work. The primary template uses type-members of the iterator type in turn
template<typename T>
struct iterator_traits {
typedef typename T::value_type value_type;
...
};
For pointers, that of course doesn't work. There is a partial specialization for them
template<typename T>
struct iterator_traits<T*> {
typedef T value_type;
...
};

- 496,577
- 130
- 894
- 1,212
-
Second point is very interesting... I had no idea, is this part of the standard or compiler specific? – DeusAduro Aug 27 '09 at 17:07
-
@DeusAduro, yes it's required by the Standard. It's a common example of a possible misconception, because it makes so `vector
` is not compliant to the container requirements anymore. – Johannes Schaub - litb Aug 27 '09 at 17:13 -
hehe, good old vector
. Lousy design decision, but it *does* illustrate some of the things that are possible with partial specializations. – jalf Aug 27 '09 at 18:31
In some stl implementations collections like std::vector
and std::list
use partial template specialization to reduce the amount of code generated for collections of pointers.
Each instantiation of a template for a type T creates new code. However pointer types are effectively all the same so generating new code for every type is a waste. This can be reduced by implementing the private part of pointer collections with void pointers and then casting these to the appropriate type in the public interface. This greatly reduces the code generated for pointer collections.
I think this is covered in Effective STL.

- 10,798
- 3
- 37
- 41
-
+1 for the hint to optimize T* (which I could also use in my own template classes) – mmmmmmmm Aug 28 '09 at 18:13
-
Wow, I didn't know about this, but sounds logical. C++ is so cool under the hood... oh wait, it has no hood by design :) – rubenvb Aug 31 '11 at 14:49
Taken from MSDN (Partial Specialization of Class Templates (C++))
// partial_specialization_of_class_templates.cpp
template <class T> struct PTS {
enum {
IsPointer = 0,
IsPointerToDataMember = 0
};
};
template <class T> struct PTS<T*> {
enum {
IsPointer = 1,
IsPointerToDataMember = 0
};
};
template <class T, class U> struct PTS<T U::*> {
enum {
IsPointer = 0,
IsPointerToDataMember = 1
};
};

- 23,277
- 13
- 73
- 121
-
1@Dwefy, I won't downvote your post :) but I am not asking about what is partial-specialization. Thanks... – Khaled Alshaya Aug 27 '09 at 16:59
-
but it looks as good sample of partial spec, even more. It injects more types into singular template, allowing you choose what is exactly to specialize – Dewfy Aug 28 '09 at 07:27