remove_all_extents源码
实现方式
通过递归调用remove_extent实现功能,获取到原始数据类型。
性能开销
模版推导过程完成功能计算,不涉及运行时开销。
调用stl内容
remove_extent
/// remove_extent
template<typename _Tp>
struct remove_extent
{ typedef _Tp type; };
template<typename _Tp, std::size_t _Size>
struct remove_extent<_Tp[_Size]>
{ typedef _Tp type; };
template<typename _Tp>
struct remove_extent<_Tp[]>
{ typedef _Tp type; };
/// remove_all_extents
template<typename _Tp>
struct remove_all_extents
{ typedef _Tp type; };
template<typename _Tp, std::size_t _Size>
struct remove_all_extents<_Tp[_Size]>
{ typedef typename remove_all_extents<_Tp>::type type; };
template<typename _Tp>
struct remove_all_extents<_Tp[]>
{ typedef typename remove_all_extents<_Tp>::type type; };
代码示例
代码源地址:http://www.cplusplus.com/reference/type_traits/remove_all_extents/
// remove_all_extents
#include <iostream>
#include <type_traits>
int main() {
typedef std::remove_all_extents<int>::type A; // int
typedef std::remove_all_extents<int[24]>::type B; // int
typedef std::remove_all_extents<int[24][60]>::type C; // int
typedef std::remove_all_extents<int[][60]>::type D; // int
typedef std::remove_all_extents<const int[10]>::type E; // const int
std::cout << std::boolalpha;
std::cout << "typedefs of int:" << std::endl;
std::cout << "A: " << std::is_same<int,A>::value << std::endl;
std::cout << "B: " << std::is_same<int,B>::value << std::endl;
std::cout << "C: " << std::is_same<int,C>::value << std::endl;
std::cout << "D: " << std::is_same<int,D>::value << std::endl;
std::cout << "E: " << std::is_same<int,E>::value << std::endl;
return 0;
}
Output:
typedefs of int:
A: true
B: true
C: true
D: true
E: false
应用场景
Obtains the type of the elements in the deepest dimension of T (if T is an array type.)
目前理解:获取多维数组的元素类型,用于非c++基本类型定义的别名的情况。
参考:gcc8源码