如何推断嵌套最多的迭代器类型
How to deduce the most nested iterator type?
我想写一个类型特征,给定一个ContainerType
,能够推断出最嵌套的IteratorType
,这意味着例如给定一个std::vector<int>
或std::vector<std::vector<int>>
或std::vector<std::vector<std::vector<int>>>
总是会推导出相同的IteratorType
,就好像它是一个std::vector<int>
一样。
在这里,我写了一个特质和一个小小的演示,它是如何工作的:
#include <type_traits>
#include <iterator>
#include <vector>
template<class ...>
using void_t = void;
template<class T, class = void>
struct is_container : std::false_type{};
template<class T>
struct is_container<T, void_t<decltype(std::begin(std::declval<T>())), decltype(std::end(std::declval<T>()))>> : std::true_type{};
template<class T, class = void>
struct drill_iterator {
using type = typename T::iterator;
};
template<class T>
struct drill_iterator<T, typename std::enable_if<is_container<typename T::value_type>::value>::type > {
using type = typename drill_iterator<typename T::value_type>::type;
};
int main(){
drill_iterator<std::vector<std::vector<int>>>::type a;
drill_iterator<std::vector<int>>::type b;
if(std::is_same<decltype(b), std::vector<int>::iterator>::value
&& std::is_same<decltype(a), std::vector<int>::iterator>::value)
return 0;
return 1;
}
在线演示
相关文章:
- 如何在c++迭代器类型中包装std::chrono
- 定义模板参数的迭代器类型
- C++如何乘以包含 std::variant 元素的向量的迭代器?正在执行迭代器类型的转换?
- C++不兼容的迭代器类型
- 如何推导 std::高级迭代器类型?
- 在 stl 容器包装器中定义迭代器类型
- 矢量迭代器类型未知
- STD ::向量迭代器类型和允许的操作
- 检查容器模板类中的迭代器类型
- 如何断言模板参数的类型 STL 迭代器类型
- 检测容器是否具有迭代器类型
- 由set2.begin()返回的迭代器类型
- 序列容器的迭代器类型是什么?
- 如何推断嵌套最多的迭代器类型
- 为什么迭代器类型与矢量容器中的 value_type * 不匹配
- ISO C++禁止声明没有自动迭代器类型的“it”
- 使用的迭代器类型错误
- 列表的迭代器类型
- 如何打印迭代器类型列表<pair<int,int>>::iterator itrator it指向的元素
- 为什么 void* 不是迭代器类型?