std::remove_all_extents
来自cppreference.com
<tbody>
</tbody>
| 在标头 <type_traits> 定义
|
||
template< class T > struct remove_all_extents; |
(C++11 起) | |
若 T 是某类型 X 的多维数组,则提供的成员 typedef type 等于 X,否则 type 等于 T。
如果程序添加了 std::remove_all_extents 的特化,那么行为未定义。
成员类型
| 名称 | 定义 |
type
|
T 的元素类型
|
辅助类型
<tbody> </tbody> template< class T > using remove_all_extents_t = typename remove_all_extents<T>::type; |
(C++14 起) | |
可能的实现
template<class T>
struct remove_all_extents { typedef T type; };
template<class T>
struct remove_all_extents<T[]> {
typedef typename remove_all_extents<T>::type type;
};
template<class T, std::size_t N>
struct remove_all_extents<T[N]> {
typedef typename remove_all_extents<T>::type type;
};
|
示例
运行此代码
#include <iostream>
#include <type_traits>
#include <typeinfo>
template<class A>
void foo(const A&)
{
typedef typename std::remove_all_extents<A>::type Type;
std::cout << "底层类型: " << typeid(Type).name() << '\n';
}
int main()
{
float a0;
float a1[1][2][3];
float a2[1][1][1][1][2];
float* a3;
int a4[3][2];
double a5[2][3];
struct X { int m; } x0[3][3];
info(a0);
info(a1);
info(a2);
info(a3);
info(a4);
info(a5);
info(x0);
}
可能的输出:
底层类型: float
底层类型: float
底层类型: float
底层类型: float*
底层类型: int
底层类型: double
底层类型: main::X
参阅
(C++11) |
检查类型是否为数组类型 (类模板) |
(C++11) |
获取数组类型的维数 (类模板) |
(C++11) |
获取数组类型在指定维数的大小 (类模板) |
(C++11) |
从给定数组类型移除一个维度 (类模板) |