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

Is there any way to get at the container template type to reuse it with another value_type?

本文关键字:另一个 value 重用 type 类型 获取 有没有      更新时间:2023-10-16

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

有什么方法可以"借用"推导出ContainerType的实际外部类模板吗?我目前有这段代码返回硬编码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中剥离的类模板.请记住,容器通常具有 1 个以上的模板参数(并且数量因容器和实现而异(,因此ContainerType的模板模板参数在这里不是解决方案。

这很糟糕,不适用于地图。我建议重新考虑您的界面。

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;
相关文章: