English 中文(简体)
减少模板政策内容
原标题:Reducing template policy clutter

如果我有以下模板参数:

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_RehashClassT_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_RehashClasT_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.

最佳回答

See e.g. Modern C++ Design (sec. 1.5.1: Implementing Policies with model-template factors). 想法是让T_RehashClass和T_HashcodeClass成为模板-典型参数。 这些班次将T_Key、T_Value和作为他们自己的参数的任何其他内容。 为了避免重整,你可以继承这些模板所需的即时使用。

template <
    typename T_Key, 
    typename T_Value, 
    typename T_HashFunc,      
    typename T_ExtractKey, 
    typename T_EqualKey, 
    template<typename, typename /*, more params */> class T_RehashClass, 
    template<typename, typename /*, more params */> class T_HashcodeClass
>   class Hashtable 
:
    public T_RehashClass<T_Key, T_Value /*, more params */>,
    public T_HashcodeClass<T_Key, T_Value /*, more params */>
{ /* ... */ }; 

NOTE: 你在T_RehashClass和T_Hashcode前确实需要“阶级”而不是“类型”。 分类因为它们是模板名称,而不是模板类型。

问题回答

我不敢肯定的是,为你的洗衣和复教课而具体说明不同的T_Key和T_Value类型是令人非常感兴趣的。 如果我正在处理这个问题,我将首先努力制定关键/高压政策。 我的倾向是说,也许应该有一种价值政策,而不是把它与关键政策混为一谈,但这里和这里都不是这样。

namespace hash {
namespace detail {

    template<
            typename Key
            , typename Value
            , typename KeyGetter
            , typename KeyComparator>
        class KeyPolicy {
        public:
            typedef Key key_t;
            typedef Value value_t;
            typedef KeyGetter get_t;
            typedef KeyComparator compare_t;
        };

}} // hash detail

<代码>HashTable是有效的,除非其具有与“后继”相同的关键政策,否则就不给予。

namespace hash {
namespace detail {

    template<typename RehashPolicy>
        class HashTableImpl {
        public:
            typedef RehashPolicy rehash_p;
            typedef typename rehash_p::key_policy_t::key_t key_t;
            // typedef ...
        };

    // this doesn t have a specific name, its anything.
    template<typename KeyPolicy>
        class SomeRehashPolicy {
        public:
            typedef KeyPolicy key_policy_t;
        };

}} // hash detail

显然,你可以补充你想要的东西。 如果我在法律审查中是一位 stick子的话,我可能会要求诸如<条码>rehash_p和<条码>key_policy_t等东西为私人。 它们实际上是执行细节。 你们试图保护的实际习惯是key_t等。

可是,在“礼让”的合理界限之外,我会说,我的诚实看法是,所有这种组合都只是对撰写的谎言感兴趣。 不是你们,也不是谁使用。 因此,我只想暴露出哈希特人组合,或者说他们实际上会使用。

namespace hash {

    struct stub {}; // sorry, I m lazy

    template<typename Key, typename Value>
        class HashTable {
        private:
            typedef typename detail::KeyPolicy<Key, Value, stub, stub> key_p;
            typedef typename detail::SomeRehashPolicy<key_p> rehash_p;
            typedef typename detail::HashTableImpl<rehash_p> impl_t;
        public:
            typedef typename impl_t::key_t key_t;
        };

} // hash

int main(int argc, char ** argv) {
    hash::HashTable<int, double> hash_table;
    return 0;
}

很显然,一些细节已经填满,但你还是有想法。





相关问题
Undefined reference

I m getting this linker error. I know a way around it, but it s bugging me because another part of the project s linking fine and it s designed almost identically. First, I have namespace LCD. Then I ...

C++ Equivalent of Tidy

Is there an equivalent to tidy for HTML code for C++? I have searched on the internet, but I find nothing but C++ wrappers for tidy, etc... I think the keyword tidy is what has me hung up. I am ...

Template Classes in C++ ... a required skill set?

I m new to C++ and am wondering how much time I should invest in learning how to implement template classes. Are they widely used in industry, or is this something I should move through quickly?

Print possible strings created from a Number

Given a 10 digit Telephone Number, we have to print all possible strings created from that. The mapping of the numbers is the one as exactly on a phone s keypad. i.e. for 1,0-> No Letter for 2->...

typedef ing STL wstring

Why is it when i do the following i get errors when relating to with wchar_t? namespace Foo { typedef std::wstring String; } Now i declare all my strings as Foo::String through out the program, ...

C# Marshal / Pinvoke CBitmap?

I cannot figure out how to marshal a C++ CBitmap to a C# Bitmap or Image class. My import looks like this: [DllImport(@"test.dll", CharSet = CharSet.Unicode)] public static extern IntPtr ...

Window iconification status via Xlib

Is it possible to check with the means of pure X11/Xlib only whether the given window is iconified/minimized, and, if it is, how?

热门标签