有没有办法获取容器模板类型以将其与另一个value_type重用
Is there any way to get at the container template type to reuse it with another value_type?
我有一个参数化的函数模板,它接受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;
相关文章:
- 删除一个线程上有数百万个字符串的大型哈希映射会影响另一个线程的性能
- 运行同一解决方案的另一个项目的项目
- 为什么在没有显式默认构造函数的情况下,将另一个结构封装在联合中作为成员的结构不能编译
- 基于另一个成员参数将函数调用从类传递给它的一个成员
- C++从另一个类访问公共静态向量的正确方法是什么
- C++-试图将函数指针推回到另一个CPP文件中的矢量时出错
- 使用std::transform将一个范围的元素添加到另一个范围中
- 输入到文件并输出到另一个文件,并将流文件传递给函数
- 我可以将一个用clang c++11编译的对象与另一个用c++17编译的对象链接起来吗
- 修改函数中的指针(将另一个指针作为参数传递)
- 为什么我不能将一个对象push_back到属于另一个类的对象向量中?
- C++试图读取一个文件并输出到另一个文本文件
- 如何将指针从一个void函数传递到另一个C++
- 如何从另一个文件继承私有成员变量和公共函数
- 使用.find函数在c++中查找字符和另一个字符之间的大小
- 在 Windows 上,是否可以让 dll 在不使用 PATH 环境变量的情况下在另一个文件夹中查找依赖项?
- 在他自己的方法中,有可能将一个对象取消引用到另一个对象吗
- C++-我可以创建另一个类的成员并在构造函数中使用它吗
- 如何将 v8::FunctionCallbackInfo<v8::Value> 数组从一个隔离复制到另一个隔离?
- 在多映射中,当两个迭代器持有具有映射到不同Value的相同键的值时.我们如何才能在地图上找到它们中的哪一个在另一个之前