如果我有以下模板参数:
template <typename T_Key, typename T_Value, typename T_HashFunc,
typename T_ExtractKey, typename T_EqualKey, typename T_RehashClass, typename T_HashcodeClass>
class Hashtable { /* ... */ };
<T_RehashClass
和T_HashcodeClass
均为的两个模板类别。 T_Key, T_Value, T_HashFunc, T_ExtractKey and T_EqualKeyHashtable
的打字清单(所有模板参数类型见<代码>Hashtable。
Note that T_RehashClass
and T_HashcodeClass
can be created by the user as well (defaults are provided) and can have other template parameters if the user wishes.
As it stands, any class that is a T_RehashClass
must have T_Key, T_Value
etc. template parameters filled out, which I see as code duplication. I would like the class to somehow have knowledge of Hashtable
so that it can access its typedefs and deduce T_Key, T_Value
etc. types automatically by creating its own typedefs. Unfortunately, in this case, I get a cyclic dependency.
这种问题如何普遍解决?
还请注意,我是在EASTL的后面,EASTL利用多个继承方式继承:T_RehashClas
和T_HashnodeClas
,因此,Hashtable
的继承范围甚至更多。 我很想知道,它周围是否有办法(即不继承<>policies>/em>,并且作为政策继承的模板参数,可以降低灵活性)。
我认为的一种解决办法是有一个模板,其所有模板参数均来自<代码>T_Key至T_EqualKey
。 <代码>Hashtable的声明将:
template <typename T_HashtableParams, typename T_RehashClass = default_rehash_class<T_HashtableParams>, typename T_HashcodeClass = default_hashnode_class<T_HashtableParams> >
class Hashtable { /* ... */ };
The T_RehashClass
and T_HashcodeClass
can then take the defaults which will eliminate code duplication. Problem with this approach is that it is more cumbersome for the user to use.