std remove_all_extents

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源码

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
【社区内容提示】社区部分内容疑似由AI辅助生成,浏览时请结合常识与多方信息审慎甄别。
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

相关阅读更多精彩内容

友情链接更多精彩内容