The new static constexpr std::integral_constant idiom
The size of std::array<T, N>
is known at compile-time given the type. Yet it only provides a regular .size()
member function:
This is annoying if you're writing generic code that expects some sort of compile-time sized range.
It would be really nice if std::array::size
were a static
member function instead:
Now we can just use ::size()
to get the size of an array without breaking existing code: You can still call static
member functions with .
syntax; foo.static_member(args)
is equivalent to std::remove_cvref_t<decltype(foo)>::static_member(args)
. There are MISRA guidelines against it and a clang-tidy check that complains, but it is really useful for generic code.
However, sometimes even Rng::size()
isn't enough. Suppose we want to have a std::integral_constant
as the result, so we can do type-based metaprogramming. Sure, we can then write std::integral_constant<std::size_t, Rng::size()>{}
, but that's a lot to type. It would be really convenient if std::array
had that directly, as that is the "most const" version of a size:
Now we can write array::size
and get a std::integral_constant
that represents the size. But we've broken all code that assumed .size()
or ::size()
?
You'd think so, but no: std::integral_constant
has a call operator!
So with a single member, we support:
array::size
, which results in astd::integral_constant
object.array::size()
, which calls theoperator()
on thestd::integral_constant
and returns astd::size_t
.array_obj.size()
, which is the same as above and returns astd::size_t
, just as a member function.
Now that is nice!
The library evolution group of the C++ committee is considering that idiom for all new standard library components with constant sizes, like std::simd
, or std::inplace_vector::capacity
(formerly std::static_vector::capacity
). Of course, we can't actually change std::array
due to ABI or something...
Do you have feedback? Send us a message at devblog@think-cell.com !
Sign up for blog updates
Don't miss out on new posts! Sign up to receive a notification whenever we publish a new article.
Just submit your email address below. Be assured that we will not forward your email address to any third party.
Please refer to our privacy policy on how we protect your personal data.