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.
Could anyone suggest why C++11 introduced this feature? Is there any benefits of using it?
There is a good example of using std::remove_extent
in the C++ Standard itself.
template function that creates an object of smart pointer std::unique_ptr
template <class T> unique_ptr<T> make_unique(size_t n);
returns the following expression (20.8.1.4 unique_ptr creation p.#4)
unique_ptr<T>(new remove_extent_t<T>[n]())
Consider for example declaration
auto p2 = std::make_unique<int[][10]>(2);
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.
When you work with arrays, you may need to get the total number of elements the array has including sub-arrays. This can be done with some template meta-programming, by using std::extent
and std::rank
along with std::remove_extent
.
template <class T, std::size_t N = std::extent<T>::value >
struct total_elements
{
using M_type = typename std::remove_extent<T>::type;
static constexpr std::size_t value = N * total_elements< M_type >::value;
};
template <class T>
struct total_elements<T, 0> : std::integral_constant<std::size_t, 1> {};
Live example.
This can be particularly useful if you work with vector
of vectors
because the storage is not guaranteed to be contiguous: by giving the total number of elements and a 1D vector
, you will obtain that. You'll also need some pointer arithmetic to make it behave as if it were multidimensional, but that's easy to abstract in a class.
As a note, the standard does not obligate you to use it: if you find a place where it may be useful, then go for it.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With