nya*_*108 3 c++ containers iterator type-traits c++11
我想编写一个类型特征,给定a ContainerType,能够推导出最嵌套的特征IteratorType,这意味着例如a std::vector<int>或a 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;
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
68 次 |
| 最近记录: |