默认从模板类中构造模板参数类型

时间:2014-08-28 16:30:01

标签: templates stl template-specialization typetraits set-union

我有以下std :: set_union< ...>的自定义特化示例。算法。我从http://en.cppreference.com/w/cpp/algorithm/set_union改编了实施。

我需要自定义实现的原因是我想从两个集合中创建元素对。在范围重叠(交集)的情况下,输出将包含std :: pair< * first_iter,* second_iter>

在元素对第一组唯一的位置,输出将在概念上是std :: pair< * first_iter,second_type()>最后,元素对第二组是唯一的,输出将是std :: pair。

代码有效,但在我的自定义中,我不得不对LoadableFile类型进行硬编码,以便为std :: pair<>的任一侧默认构造元素。 (取决于上述专业化的3个案例中的2个)。是否有某种方法可以从Merge模板参数中访问类型信息以了解其类型(它是 PairMaker<可加载,可加载> 和默认构造Loadable()&s;而无需求助硬编码专业化?

这是我的自定义:

// customized set_union that merges elements from both sets
// when the elements are unique in InputIt1 - merge constructs
// a pair with a default constructed type of InputIt1
template<typename InputIt1, typename InputIt2,
    typename OutputIt, typename Compare, 
    typename Merge>
OutputIt custom_set_union(
    InputIt1 first1, InputIt1 last1,
    InputIt2 first2, InputIt2 last2,
    OutputIt d_first, Compare comp,
    Merge merge)
{
    // example implementation taken and modified from cppreference.com
    for (; first1 != last1; ++d_first) {
        // empty second set
        if (first2 == last2) {
            // return std::copy(first1, last1, d_first);
            // equivalent of std::copy(...) from cppreference.com
            while (first1 != last1) {
                //*d_first++ = *first++;
                *d_first++ = merge(*first1++, LoadableFile());
            }
            return d_first;
        }
        if (comp(*first2, *first1)) {
            //*d_first = *first2++;
            *d_first = merge(LoadableFile(), *first2++);
        } else {
            //*d_first = *first1;
            // @JC note added *first2 as merge arg2 - overlapping region
            *d_first = merge(*first1, *first2);
            if (!comp(*first1, *first2))
                ++first2;
            ++first1;
        }
    }
    // return std::copy(first2, last2, d_first);
    // equivalent of std::copy(...) from cppreference.com
    while (first2 != last2) {
        //*d_first++ = *first++;
        *d_first++ = merge(LoadableFile(), *first2++);
    }
    return d_first;
};

为了提供所需的框架来使元素适合OutputIt - 我有一个Merge类,它使得Output适合OutputIt,如下所示:

/**
 * PairMaker helper struct to make pairs of objects from
 * 2 different types of sets templated on types A & B
 * must have default constructors - pair types A & B must
 * have default constructors
 */
template<typename A, typename B>
struct PairMaker {
    std::pair<A, B> operator() (const A& a, const B& b) const {
        return std::make_pair(a, b);
    }
};

在我的情况下实际使用我称之为的专业化如下:

    auto comp = [](const LoadableFile& lhs, const LoadableFile& rhs) {
        return lhs.getRelativePath().filename() < rhs.getRelativePath().filename();
    };

    PairMaker<LoadableFile, LoadableFile> pairMaker;
    std::set<std::pair<LoadableFile,LoadableFile>> resultSet;
    custom_set_union(rLocalFileInfo.cbegin(), rLocalFileInfo.cend(),
        rModuleFileInfo.cbegin(), rModuleFileInfo.cend(), 
        std::inserter(resultSet, resultSet.end()),
        comp, pairMaker);

0 个答案:

没有答案
相关问题