什么可以std :: remove_extent用于?
我正在研究C ++ 11提供的新功能,并发现 std :: remove_extent 。
I'm studying new features provided by C++11 and I found std::remove_extent.
typedef std::remove_extent<int[24]>::type A; // A is int
但是我找不到这个用法,除了定义一个新的
However I can't find a usage for this, aside from defining a new type from an existing type by removing a dimension from the given type.
任何人都可以建议为什么C ++ 11引入了这个特性?使用它有什么好处吗?
Could anyone suggest why C++11 introduced this feature? Is there any benefits of using it?
有一个很好的例子使用 std :: remove_extent
There is a good example of using std::remove_extent
in the C++ Standard itself.
模板函数创建智能指针的对象 std :: unique_ptr
template function that creates an object of smart pointer std::unique_ptr
template <class T> unique_ptr<T> make_unique(size_t n);
返回以下表达式(20.8.1.4 unique_ptr creation p。#4)
returns the following expression (20.8.1.4 unique_ptr creation p.#4)
unique_ptr<T>(new remove_extent_t<T>[n]())
考虑例如声明
auto p2 = std::make_unique<int[][10]>(2);
在这种情况下,make_unique需要删除 []
在 int [] [10]
中替换 [2]
c $ c> int [2] [10] 在新表达式中。
In this case make_unique need to remove dimension specified like []
in the type declaration int[][10]
and substitute it for [2]
getting int[2][10]
in the new expression.