C++11 模板参数"late binding"

C++11 "late binding" of template arguments

本文关键字:late binding 参数 C++11      更新时间:2023-10-16

请不要误解我的"延迟绑定",我不是指运行时通常的延迟绑定,我指的是其他东西,找不到更好的词来形容它:

假设我正在为某个值类型V开发一个容器(或类似的)数据结构Containor,它需要将这些值与比较器进行比较,所以我的第一个模板看起来像这个

template<typename Val, typename Comp = std::less<Val>>
struct Containor{};

现在,我的Containor结构在内部使用了另一个容器。要使用的容器也应该可以通过模板参数进行配置,比如说默认值是std::set。所以我的Containor的下一个版本是这样的:

template<typename Val, typename Comp = std::less<Val>, typename Cont = std::set<Val,Comp>>
struct Containor{};

这里是代码开始闻IMHO的地方。只要用户对内部容器的默认实现感到满意,一切都很好。但是,假设他想使用新的googlebtree集实现btree::btree_set而不是std::set。然后他必须安装这样的模板:

typedef Containor<int,std::less<int>,btree::btree_set<int,std::less<int>> MyContainor;
                                                     ^^^^^^^^^^^^^^^^^^^

我已经强调了我的问题所在。客户端代码必须使用正确的参数设置btree_set。这真的很糟糕,因为Containor类总是需要一组与它自己的前两个模板参数完全相同的类型和比较器。客户端可以在此处插入其他类型的内容,这是偶然的!此外,客户有责任选择正确的参数。在这种情况下,这可能很容易,但如果内部容器必须是一组值类型和其他类型的对,就很难了。然后,客户端要想正确地获取内部集合的类型参数就更加困难了。

因此,我想要的是一种客户端代码只提交原始模板,Containor在内部用正确的参数对其进行初始化的方式,即:

template<typename Val, typename Comp = std::less<Val>, typename Cont = std::set >
struct Containor{
    typedef Cont<Val,Comp> innerSet; 
//  ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ container instanciates the inner containor
};
typedef Containor<int,std::less<int>,btree::btree_set> MyContainor;
//                                   ^^^^^^^^^^^^^^^^
//                         client only hands in  raw template

当然,这不是有效的C++!

所以我想办法解决这个问题。我能想到的唯一解决方案是为我想使用的所有数据结构编写"绑定类",比如:

struct btree_set_binder{
    template<typename V, typename C = std::less<V>>
    struct bind{
        typedef btree::btree_set<V,C> type;
    }
};

现在我可以用一个固定的活页夹定义我的Containor

template<typename Val, typename Comp = std::less<Val>, typename ContBinder = btree_set_binder >
struct Containor{
    typedef btree_set_binder::bind<Val,Comp>::type innerSet; 
//          ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ works like a charm
};

现在,用户必须只提供所需的绑定器类,Containor将使用正确的参数来初始化它。所以这些活页夹类对我来说是可以的,但为所有容器编写活页夹类是一件很麻烦的事。那么,有没有更好或更简单的方法在C++11中"晚些时候"绑定模板参数,即在另一个检索原始模板作为参数的模板中。

也许可以制作自己的比较器特性。

// Comparator trait primary template
template <typename T> stuct MyComparator
{
    typedef typename T::key_compare type;
};
// Comparator trait non-standard usage example
template <typename U, typename V, int N>
struct MyComparator<WeirdContainer<U, V, N>>
{
    typedef std::greater<V> type;
};
template <typename T, typename Cont = std::set<T>>
struct MyAdaptor
{
    typedef typename MyComparator<Cont>::type comparator_type;
    typedef T value_type;
    // ...
};

我已经将您的"Containor"重命名为"MyAdaptor",因为这种构造通常被称为"适配器"类。

用法:

MyAdaptor<int> a;    // uses std::set<int> and std::less<int>
MyAdaptor<double, WeirdContainer<bool, double, 27>> b;

更新:根据讨论,您甚至可以完全删除外部类型参数:

template <typename Cont> struct MyBetterAdaptor
{
    typedef MyAdaptorTraits<Cont>::value_type value_type;
    typedef MyAdaptorTraits<Cont>::pred_type pred_type;
    // ...
};

这样使用:

MyBetterAdaptor<std::set<int>> c; // value type "int", predicate "std::less<int>"

编写MyAdaptorTraits模板只是一个练习。

所以我想要的是一种客户端代码只提交原始代码的方式模板,Containor在内部用正确的自变量,

因此,您需要的显然是一个模板模板参数:

// std::set has three template parameters, 
// we only want to expose two of them ...
template <typename V, typename C>
using set_with_defalloc = std::set<V,C>;
template<
    typename Val,
    typename Comp = std::less<Val>, 
    template <typename V, typename C> class Cont = set_with_defalloc>
struct Containor{
    typedef Cont<Val,Comp> innerSet; 
    // ...
};

您应该能够使用template template参数来执行此操作,如:

template<typename Val, typename Comp = std::less<Val>, template <typename...> class ContBinder = std::set>
    struct Containor {
        typedef ContBinder<Val, Comp> innerSet;
        // ...
    };

注意:您需要可变的typename...,因为std::set有三个模板参数(第三个是分配器),而其他容器可能没有。