You appear to be confusing partial specialization with default template arguments. It further appears you need both (for reasons unstated, but not really important). Though not entirely intuitive, it can be accomplished as follows:
#include <iostream>
template <typename Key, typename Value = bool>
class KeyValuePair
{
public:
KeyValuePair()
{
std::cout << __PRETTY_FUNCTION__ << ':' << "original\n";
}
};
template <typename Key>
class KeyValuePair<Key, bool>
{
public:
KeyValuePair()
{
std::cout << __PRETTY_FUNCTION__ << ':' << "specialized\n";
}
};
int main()
{
KeyValuePair<int,int> kvp1;
KeyValuePair<int> kvp2;
}
Output
KeyValuePair<int, int>::KeyValuePair() [Key = int, Value = int]:original
KeyValuePair<int, bool>::KeyValuePair() [Key = int, Value = bool]:specialized
The confusing part to some is the default argument specification, but a template that follows which will never actually see fruition with that default-arg because of the later specialization. In such cases I prefer to forward-declare the template with its default argument list. At least for me it makes it slightly easier to read. You may (or not) choose to do the same if you feel it offers clarity. Something like:
template <typename Key, typename Value = bool>
class KeyValuePair;
template <typename Key, typename Value>
class KeyValuePair
{
public:
KeyValuePair()
{
std::cout << __PRETTY_FUNCTION__ << ':' << "original\n";
}
};
template <typename Key>
class KeyValuePair<Key, bool>
{
public:
KeyValuePair()
{
std::cout << __PRETTY_FUNCTION__ << ':' << "specialized\n";
}
};
Valueto be inKeyValuePair<int>? - TartanLlamaKeyValuePair<int>, I expectValueto bebool, but I did manifestly not define the partial specialization properly to do so. - shriketemplate <typename Key, typename Value = bool> class KeyValuePair {};- TartanLlama