Namespaces
Variants

std:: remove_extent

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
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_extent ;
(C++11 起)

如果 T 是某种类型 X 的数组,则提供成员类型定义 type 等于 X ,否则 type T 。请注意,如果 T 是多维数组,仅会移除第一维度。

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

目录

成员类型

名称 定义
type T 元素的类型

辅助类型

template < class T >
using remove_extent_t = typename remove_extent < T > :: type ;
(C++14 起)

可能的实现

template<class T>
struct remove_extent { using type = T; };
template<class T>
struct remove_extent<T[]> { using type = T; };
template<class T, std::size_t N>
struct remove_extent<T[N]> { using type = T; };
(注:根据要求,HTML标签、属性及 标签内的C++代码均未翻译,仅翻译了表格外的说明文本。由于表格内全部为代码块,故无需翻译内容)

示例

#include <algorithm>
#include <iostream>
#include <iterator>
#include <type_traits>
template<class A>
    std::enable_if_t<std::rank_v<A> == 1>
print_1d(const A& a)
{
    std::copy(a, a + std::extent_v<A>,
        std::ostream_iterator<std::remove_extent_t<A>>(std::cout, " "));
    std::cout << '\n';
}
int main()
{
    int a[][3] = {{1, 2, 3}, {4, 5, 6}};
//  print_1d(a); // 编译时错误
    print_1d(a[1]);
}

输出:

4 5 6

参见

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