什么可以使用std :: remove_extent?

xua*_*yue 6 c++ c++11

我正在研究C++ 11提供的新功能,我找到了std :: remove_extent.

typedef std::remove_extent<int[24]>::type A; // A is int
Run Code Online (Sandbox Code Playgroud)

但是,除了通过从给定类型中删除维度从现有类型定义新类型之外,我找不到此用法.

有谁能建议为什么C++ 11引入了这个功能?使用它有什么好处吗?

Vla*_*cow 10

std::remove_extent在C++标准本身中有一个很好的使用示例.

模板函数,用于创建智能指针的对象 std::unique_ptr

template <class T> unique_ptr<T> make_unique(size_t n);
Run Code Online (Sandbox Code Playgroud)

返回以下表达式(20.8.1.4 unique_ptr creation p.#4)

unique_ptr<T>(new remove_extent_t<T>[n]())
Run Code Online (Sandbox Code Playgroud)

考虑例如声明

auto p2 = std::make_unique<int[][10]>(2);
Run Code Online (Sandbox Code Playgroud)

在这种情况下,make_unique需要删除[]类型声明中指定的维度,int[][10]并将其替换为[2]获取int[2][10]新表达式.