是否可以在 c++ 中提取容器模板类
Is it possible to extract in c++ the container template class?
我想知道是否可以检测模板类容器类型,并重新定义其参数。例如:
typedef std::vector<int> vint;
typedef typeget<vint>::change_param<double> vdouble;
vdouble现在在哪里会成为std::vector<double>
?
除了@Kerrek SB的答案之外,这里是通用方法:
template<typename...> struct rebinder;
template<template<typename...> class Container, typename ... Args>
struct rebinder<Container<Args...>>{
template<typename ... UArgs>
using rebind = Container<UArgs...>;
};
这将适用于阳光下的任何容器。
是的,您可以使用部分专用化制作一个简单的模板重新绑定器:
#include <memory>
#include <vector>
template <typename> struct vector_rebinder;
template <typename T, typename A>
struct vector_rebinder<std::vector<T, A>>
{
template <typename U>
using rebind =
std::vector<U,
typename std::allocator_traits<A>::template rebind_alloc<U>>;
};
用法:
using T1 = std::vector<int>;
using T2 = vector_rebinder<T1>::rebind<double>;
现在T2
std::vector<double>
.
相关文章:
- 从包含m行的文件中提取n行,必要时(惰性地)重复该文件
- 如何从 std::atomic 中提取指针 T<T>?
- 为什么istream不支持右值提取
- 如何设置一个范围来提取我想要获得的信息
- 视觉工作室项目.提取源文件夹名称
- C++17 - 使用自定义分配器的节点提取/重新插入 - 适用于 clang++/libc++,但不适用于 libstd
- 从字符串中提取整数并形成一个数组
- C ++中的StringStream有助于使用向量从字符串中提取逗号分隔的整数,而不是空格分隔的整数,为什么?
- asn1c 不会从 asn.1 模块中提取八位字节字符串的默认值
- 从 std::vector<无符号字符>切片中提取 int?
- 在C++中使用重载提取运算符时出现问题
- 在 Qt(C++) 中使用 QProcess 解压缩 - 提取目录问题
- C++(.cpp文件和.h文件)拆分代码并添加一个函数,提取 - 这很容易吗?
- 无法从 std::string 中提取C++ Unicode 符号
- 如何从也包含C++字母的文本文件中提取某些数字?
- 如何从适合一定范围的数字中提取数字?
- C++从通用引用中提取实际类型
- 从运行服务的应用程序代码中提取窗口句柄
- 如何在字符串中找到字符,然后在C++中提取其余的字符串
- std::unordered_map::提取引用/指针失效