有没有办法获取容器模板类型以重用它与另一个value_type?

时间:2018-01-13 15:34:43

标签: c++ templates containers c++17

我在typename ContainerType上有一个参数化的函数模板,它接受const ContainerType&参数。它将容器内的每个元素转换为其他类型,并且(当前)返回转换类型的std::vector

有什么方法可以“借用”推导出的ContainerType的实际外类模板? 我目前有this code returning the hardcoded vector

template<typename ContainerType, typename OutputType = decltype(convert(std::declval<typename ContainerType::value_type>()))>
std::vector<OutputType> convert(const ContainerType& input)
{
  std::vector<OutputType> result;
  result.reserve(input.size());
  std::transform(input.begin(), input.end(),
                 std::back_inserter(result),
                 static_cast<OutputType(*)(typename ContainerType::value_type)>(&convert));
  return result;
}

我想推断仅从value_type剥离的类模板。请记住,容器通常具有多个模板参数(并且数量因容器和实现而不同),因此ContainerType的模板模板参数不是解决方案。

1 个答案:

答案 0 :(得分:1)

这很黑,因为地狱并没有使用地图。我建议重新考虑你的界面。

template<bool isAlloc, class From, class Of, class With>
struct rebind_arg { using type = From; };

template<bool isAlloc, class From, class Of, class With>
using rebind_arg_t = typename rebind_arg<isAlloc, From, Of, With>::type;

// allocators need to be rebound with allocator_traits
template<class From, class Of, class With>
struct rebind_arg<true, From, Of, With> { 
    using type = typename std::allocator_traits<From>::template rebind_alloc<With>; 
};

// Try to rebind non-allocator arguments
template<class T, class With>
struct rebind_arg<false, T, T, With> { using type = With; };

template<template<class...> class X, class T, class... Args, class With>
struct rebind_arg<false, X<Args...>, T, With> { 
    using type = X<rebind_arg_t<false, Args, T, With>...>; 
};

// resolve an ambiguity
template<template<class...> class X, class... Args, class With>
struct rebind_arg<false, X<Args...>, X<Args...>, With> { 
    using type = With; 
};

// Obtain the container's allocator type if it has one
template<class T, class=void>
struct get_allocator_type { struct none; using type = none; }; 

template<class T>
struct get_allocator_type<T, std::void_t<typename T::allocator_type>> { 
    using type = typename T::allocator_type; 
};

// Check if a type is the allocator type of another type
template<class T, class C>
constexpr bool is_allocator_of = std::is_same_v<T, typename get_allocator_type<C>::type>;

template<class C, class With>
struct rebind_container;

template<template<class...> class X, class... Args, class With>
struct rebind_container<X<Args...>, With> {
   using Source = X<Args...>;
   using type = X<rebind_arg_t<is_allocator_of<Args, Source>, Args,
                               typename Source::value_type, With>...>;
   static_assert(!std::is_same_v<Source, type>, "Rebinding unsuccessful");
};

template<class C, class With>
using rebind_container_t = typename rebind_container<C, With>::type;