我想定義一個適用於給定基類的所有子類的C++模板特化。這可能嗎?所有小類的模板特化
特別是,我想這樣做STL的散列<>。哈希<>被定義爲一個空的參數化模板,爲特定類型的家庭專業化的:
template<class _Key>
struct hash { };
template<>
struct hash<char>
{
size_t
operator()(char __x) const
{ return __x; }
};
template<>
struct hash<int>
{
size_t
operator()(int __x) const
{ return __x; }
};
...
我想定義是這樣的:
template<class Base>
struct hash {
size_t operator()(const Base& b) const {
return b.my_hash();
}
};
class Sub : public Base {
public:
size_t my_hash() const { ... }
};
,並能夠使用它像這個:
hash_multiset<Sub> set_of_sub;
set_of_sub.insert(sub);
但是,我的哈希模板與STL的通用模板衝突。有沒有一種方法(可能使用特徵)來定義適用於給定基類的所有子類的模板特化(不修改STL定義)?
注意,我知道我可以在每次需要這個哈希專業化一些額外的模板參數做到這一點,但我想避免這種可能的話:
template<>
struct hash<Base> {
size_t operator()(const Base& b) const {
return b.my_hash();
}
};
....
// similar specialization of equal_to is needed here... I'm glossing over that...
hash_multiset<Sub, hash<Base>, equal_to<Base> > set_of_sub;
set_of_sub.insert(sub);
如果其衝突爲什麼不使用命名空間? – Arunmu
http://stackoverflow.com/questions/1032973/how-to-partially-specialize-a-class-template-for-all-derived-types –
可能的重複...其中解決方案是命名所有派生類以類似的方式,這不是很滿意。 – jpalecek