Namespaces
Variants

std:: remove_all_extents

From cppreference.net
Metaprogramming library
Type traits
Type categories
(C++11)
(C++11) ( DR* )
Type properties
(C++11)
(C++11)
(C++14)
(C++11) (deprecated in C++26)
(C++11) ( until C++20* )
(C++11) (deprecated in C++20)
(C++11)
Type trait constants
Metafunctions
(C++17)
Supported operations
Relationships and property queries
Type modifications
remove_all_extents
(C++11)

Type transformations
(C++11) (deprecated in C++23)
(C++11) (deprecated in C++23)
(C++11)
(C++11) ( until C++20* ) (C++17)

Compile-time rational arithmetic
Compile-time integer sequences
定义于头文件 <type_traits>
template < class T >
struct remove_all_extents ;
(C++11 起)

如果 T 是某种类型 X 的多维数组,则提供成员类型定义 type 等于 X ,否则 type T

如果程序为 std::remove_all_extents 添加特化,则行为未定义。

目录

成员类型

名称 定义
type T 元素的类型

辅助类型

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 info(const A&)
{
    typedef typename std::remove_all_extents<A>::type Type;
    std::cout << "underlying type: " << 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);
}

可能的输出:

underlying type: float
underlying type: float
underlying type: float
underlying type: float*
underlying type: int
underlying type: double
underlying type: main::X

参见

(C++11)
检查类型是否为数组类型
(类模板)
(C++11)
获取数组类型的维度数量
(类模板)
(C++11)
获取数组类型在指定维度上的大小
(类模板)
从给定数组类型移除一个维度
(类模板)