15

Given a builtin array x of arbitrary type T, there are functions std::begin() and std::end() that I can call, but why isn't there a std::size()? Seems odd not to have that.

I could use std::end(x)-std::begin(x), but still a std::size(x) would be better.

Yes, I know of the std::vector and std::array classes. This is just a question of why something as simple as this isn't available as yet in the STL.

16
  • There is std::distance, similar to what your looking for
    – aaronman
    Oct 8, 2013 at 20:54
  • Should std::size only work for a type if it is cheap, or should it work regardless? Oct 8, 2013 at 21:15
  • 1
    @Gerald Then why bother with std::begin() and std::end()?
    – Adrian
    Oct 8, 2013 at 22:15
  • 2
    @Gerald: That's a weak argument, one could add std::valarray::begin. The only container for which you can't add such a member is T[].
    – MSalters
    Oct 8, 2013 at 22:25
  • 1
    You can use boost::size() stackoverflow.com/a/8266602/61505
    – KindDragon
    Sep 10, 2014 at 9:16

4 Answers 4

28

Just a note to let people know that N4280 "Non-member size() and more (Revision 2)" has been accepted into the C++17 Working Draft. This includes std::size() as well as std::empty() and std::data().

5
  • Great. Do you know, why does template <class T, size_t N> constexpr bool empty(const T (&array)[N]) noexcept; return only false? N could be zero. Oct 23, 2015 at 14:36
  • @user2023370, int ar[0]; would not compile.
    – Ajay
    Jun 9, 2016 at 11:29
  • @Ajay how about int ar[] = {};? Jun 23, 2017 at 20:54
  • @MarkRansom, Doesn't compile either, since elements would be zero.
    – Ajay
    Jun 24, 2017 at 6:07
  • @MarkRansom, because zero-sized compile time array is non-standard, otherwise this also won't compile: ideone.com/wzhkiV
    – Ajay
    Jun 24, 2017 at 18:04
16

There's std::extent, which is to be applied to the type of the array:

#include <type_traits>

int a[12];

assert(std::extent<decltype(a)>::value == 12);

Alternatively you can use std::distance(std::begin(a), std::end(a)).

The former is manifestly a constant expression, though in practice the latter can be comuted statically as well.

Finally, there's always the homegrown solution:

template <typename T, std::size_t N>
constexpr std::size_t array_size(T const (&)[N])
{ return N; };
8
  • 2
    Yeah, I know about the homegrown version. I was just wondering why it wasn't in the STL. Thanks for the std::extent method.
    – Adrian
    Oct 8, 2013 at 21:08
  • @Adrian: I suppose there isn't such a general use for this... most times you need the size, you want to iterate over the array, which you can do in other ways. But, maybe it'll be added at some point.
    – Kerrek SB
    Oct 8, 2013 at 21:27
  • 1
    std::size() would be useful if one wanted to declare multiple arrays of the same length in which the length of the first was determined via list-initialization. Aug 10, 2014 at 23:44
  • 6
    @JoshuaGreen: There's a pending proposal for a free std::size function (as well as std::empty).
    – Kerrek SB
    Aug 11, 2014 at 12:18
  • 1
    @Azeem: well, the question is tagged as c++11 :-)
    – Kerrek SB
    Aug 9, 2018 at 19:44
2

STL algorithm works on iterators, not on any container, size of a STL container would need a start and end, that won't make sense. For such we already have std::distance

8
  • One can do: int x[] = {1,2,3,4,5}; for_each(std::begin(x), std::end(x), [](int element) { std::cout << element << std::endl; }); Which shows it can use STL algorithms.
    – Adrian
    Oct 8, 2013 at 20:59
  • 1
    @Adrian: Why not for (int element : x) std::cout << element << std::endl;?
    – Kerrek SB
    Oct 8, 2013 at 21:00
  • @Adrian I'm sorry I didn't get the context, what can use STL algorithm ?
    – P0W
    Oct 8, 2013 at 21:01
  • @KerrekSB Yeah, that is also another option.
    – Adrian
    Oct 8, 2013 at 21:02
  • @POW, I actually didn't say use a STL algorithm in my question, but arrays can be used as a container in the context of the STL algorithm library using a pointer as the iterator. std::begin(x) is equivalent to std::vector y; y.begin().
    – Adrian
    Oct 8, 2013 at 21:06
0

I suppose you mean C-like arrays. The answer, as Bjarne Stroustrup said, is because "An array in C is a data type that is so stupid that it didn't even know how many elements it got." Once an array decays to a pointer there is no way to know how many elements where there in the 'array'.

4
  • Where did Bjarne Stroustrup say that? A quick search doesn't bring up that quote anywhere. It seems bogus to me: the information remains available so long as you work with the array type. Only once you stop working with the array type, the information is lost.
    – user743382
    Oct 8, 2013 at 21:10
  • 1
    Bjarne Stroustrup - The Essence of C++: With Examples in C++84, C++98, C++11, and C++14 at GoingNative 2013 channel9.msdn.com/Events/GoingNative/2013/… at 49:19. Oct 8, 2013 at 21:15
  • Thanks. No wonder a search didn't bring that up, if it hasn't been transcribed. :) It still seems bogus to me, though.
    – user743382
    Oct 8, 2013 at 21:18
  • 1
    But you can easily get the size by not allowing it to decay into a pointer (which is what std::begin and std::end do). Oct 8, 2013 at 21:23

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.