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 Commented Oct 8, 2013 at 20:54
  • Should std::size only work for a type if it is cheap, or should it work regardless? Commented Oct 8, 2013 at 21:15
  • 1
    @Gerald Then why bother with std::begin() and std::end()? Commented 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[]. Commented Oct 8, 2013 at 22:25
  • 1
    You can use boost::size() stackoverflow.com/a/8266602/61505 Commented 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().

Sign up to request clarification or add additional context in comments.

5 Comments

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.
@user2023370, int ar[0]; would not compile.
@Ajay how about int ar[] = {};?
@MarkRansom, Doesn't compile either, since elements would be zero.
@MarkRansom, because zero-sized compile time array is non-standard, otherwise this also won't compile: ideone.com/wzhkiV
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 Comments

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: 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.
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.
@JoshuaGreen: There's a pending proposal for a free std::size function (as well as std::empty).
@Azeem: well, the question is tagged as c++11 :-)
|
3

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 Comments

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: Why not for (int element : x) std::cout << element << std::endl;?
@Adrian I'm sorry I didn't get the context, what can use STL algorithm ?
@KerrekSB Yeah, that is also another option.
@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().
|
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 Comments

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.
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.
Thanks. No wonder a search didn't bring that up, if it hasn't been transcribed. :) It still seems bogus to me, though.
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).

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.