std::remove_all_extents_C++中文网

template< class T >
struct remove_all_extents;

(C++11 起)

T 是某类型 X 的多维数组,则提供等于 X 的成员 typedef type ,否则 typeT

添加 remove_all_extents 的特化的程序行为未定义。

成员类型

辅助类型

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 << "underlying type: " << typeid(Type).name() << '\n';
}
 
int main()
{
    float a1[1][2][3];
    int a2[3][2];
    float a3[1][1][1][1][2];
    double a4[2][3];
 
    foo(a1);
    foo(a2);
    foo(a3);
    foo(a4);
}

可能的输出:

underlying type: f
underlying type: i
underlying type: f
underlying type: d

参阅