如何让模板函数在适当的时候在插入器上使用 back_inserter

How to get template function to use back_inserter over inserter when appropriate

如何创建一个将一个集合的内容添加到另一个集合的函数,如果可能的话使用 std::back_inserter() 以提高效率?我没有看到 push_back() 的明显特征,我也不是 std::enable_if 的专家,但我希望某种组合能够达到以下效果:

// IF HAS_PUSH_BACK:
template<typename CIn, typename COut>
void addAll(CIn && from, COut && to) {
    std::copy(std::begin(from), std::end(from), std::back_inserter(to));
}

// IF ! HAS_PUSH_BACK:
template<typename CIn, typename COut>
void addAll(CIn && from, COut && to) {
    std::copy(std::begin(from), std::end(from), std::inserter(to, to.begin()));
}

How do I create a function that adds the contents of one collection to another, using back_inserter if possible for efficiency?

我想你可以声明一个模板函数 return std::true_typepush_back()

template <typename T>
constexpr auto hasPushBack (int)
   -> decltype( std::declval<T>().push_back(*(std::declval<T>().begin())),
                std::true_type() );

以及 return std::false_type

的故障恢复功能
template <typename>
constexpr std::false_type hasPushBack (long);

所以你可以修改你的函数如下

template<typename CIn, typename COut>
typename std::enable_if<true == decltype(hasPushBack<COut>(0))::value>::type
   addAll (CIn && from, COut && to)
 { std::copy(std::begin(from), std::end(from), std::back_inserter(to)); }

template<typename CIn, typename COut>
typename std::enable_if<false == decltype(hasPushBack<COut>(0))::value>::type
   addAll(CIn && from, COut && to)
 { std::copy(std::begin(from), std::end(from), std::inserter(to, to.begin())); }

如果您可以使用 C++14 或更新版本,您还可以定义一个具有值的模板变量

template <typename T>
constexpr bool hasPushBack_v = decltype(hasPushBack<T>(0))::value;

并且可以将函数简化如下

template<typename CIn, typename COut>
std::enable_if_t<true == hasPushBack_v<COut>>
   addAll (CIn && from, COut && to)
 { std::copy(std::begin(from), std::end(from), std::back_inserter(to)); }

template<typename CIn, typename COut>
std::enable_if_t<false == hasPushBack_v<COut>>
   addAll(CIn && from, COut && to)
 { std::copy(std::begin(from), std::end(from), std::inserter(to, to.begin())); }

您可以申请SFINAE with the help of std::enable_if and std::void_t

template <typename T, typename = void>
struct has_push_back : std::false_type {};
template <typename T>
struct has_push_back<T, std::void_t<decltype(std::declval<T>().push_back(std::declval<typename T::value_type>()))>>
    : std::true_type {};

// IF HAS_PUSH_BACK:
template<typename CIn, typename COut>
std::enable_if_t<has_push_back<std::remove_reference_t<COut>>::value> addAll(CIn && from, COut && to) {
    std::copy(std::begin(from), std::end(from), std::back_inserter(to));
}

// IF ! HAS_PUSH_BACK:
template<typename CIn, typename COut>
std::enable_if_t<!has_push_back<std::remove_reference_t<COut>>::value> addAll(CIn && from, COut && to) {
    std::copy(std::begin(from), std::end(from), std::inserter(to, to.begin()));
}

LIVE

只是为了好玩,从 C++14 开始你也可以使用变量模板

template <class...> using void_t = void; // (compensate C++14 lack)

template <class T, class = void>
constexpr bool HasPushBack{false};

template <class T>
constexpr bool HasPushBack<T, void_t<
    decltype(std::declval<T>().push_back(std::declval<typename std::decay_t<T>::value_type>()))>
>{true};

template<typename CIn, typename COut, std::enable_if_t< HasPushBack<COut>,bool> = true>
void addAll(CIn && from, COut && to) {
    std::copy(std::begin(from), std::end(from), std::back_inserter(to));
}

template<typename CIn, typename COut, std::enable_if_t<!HasPushBack<COut>,bool> = true>
void addAll(CIn && from, COut && to) {
    std::copy(std::begin(from), std::end(from), std::inserter(to, to.begin()));
}