将N种类型的参数包折叠成N-1对

时间:2018-02-24 17:35:12

标签: c++ c++11 c++17

我正在尝试将N-1种不同类型的参数包折叠成std::pairsResolveToTupleOfPairs<void, int, long>::Type tuple; std::tuple<std::pair<void, int>, std::pair<int, long>> tuple; 各种类型。

所以例如表达式

ResolveToTupleOfPairs

应评估为

<T0, T1>, <T1, T2>, ...

所以我正在搜索template<typename... T> struct ResolveToTupleOfPairs { static_assert(sizeof...(Args) > 1, "need at least two arguments"); using Type = std::tuple<std::pair<T, T>...>; }; 类型的实现来折叠参数包,如上所述。我当前的实现如下,但显然它导致类型为对的元组,每个对包含两次相同的类型,而不是c++17

$productnotification = [];
foreach ($apiproducts as  $api)
{   
    foreach ($myproduct as  $product)
    {
        if($product['name'] == $api['name'] && $api['price'] > $product['price'])
        array_push($productnotification, $product["name"]);
    }    
}

我对/** * DELETE /bank-accounts/:id : delete the "id" bankAccount. * * @param id the id of the bankAccount to delete * @return the ResponseEntity with status 200 (OK) */ @DeleteMapping("/bank-accounts/{id}") @Timed public ResponseEntity<Void> deleteBankAccount(@PathVariable Long id) { log.debug("REST request to delete BankAccount : {}", id); bankAccountRepository.delete(id); return ResponseEntity.ok().headers(HeaderUtil.createEntityDeletionAlert(ENTITY_NAME, id.toString())).build(); } 解决方案很好。

1 个答案:

答案 0 :(得分:5)

我们利用参数包扩展真的很聪明的事实

template<typename...>
struct fold;

template<size_t... Is, typename... Ts>
struct fold<std::index_sequence<Is...>, Ts...>
{
    using tuple = std::tuple<Ts...>;
    using type = std::tuple<std::pair<std::tuple_element_t<Is, tuple>,
                                      std::tuple_element_t<Is + 1, tuple>>...>;
};

template<typename... Ts>
using fold_t = typename fold<std::make_index_sequence<sizeof...(Ts) - 1>, Ts...>::type;

Live