What is the purpose of std::array?

Started by
6 comments, last by SiCrane 11 years, 1 month ago

I thought I would try it and I found it... superfluous. huh.png What am I missing? All it seems to be is a wrapper surrounding a standard C array. It doesn't seem extend array functionality beyond what is already possible in C/C++, Why include a file and have to compile a couple hundred lines of code when "std::array<int,10> a;" is equivalent to "int a[10];"? Very Rube Goldberg-ish.

I am assuming there is a point to this?dry.png

Advertisement

Try passing int a[10] to a function and getting its size ;)

Also, you can do something like this:


template <typename T>
T doubleData(const T& v)
{
    T data = v; // Can't do this with an int a[10]
    for (typename T::value_type& value : data) // int a[10] certainly has no ::value_type member type
        value *= 2;

    return data; // Can't do this with an int a[10] either!
}
 
// You can call the above function with std::vector, std::array, etc., but not with int a[10]!

Edit: Also, check out this article

Edit edit: good catch, Brother Bob!

[size=2][ I was ninja'd 71 times before I stopped counting a long time ago ] [ f.k.a. MikeTacular ] [ My Blog ] [ SWFer: Gaplessly looped MP3s in your Flash games ]

Try passing int a[10] to a function and getting its size wink.png

You can do something like this:


template <typename T>
void printAll(const T& v)
{
    for (auto& value : v)
        std::cout << value << std::endl;
}
 
// You can call the above function with std::vector, std::array, etc., but not with int a[10]!
Edit: Also, check out this article

You may want to reconsider that example. The array is passed by reference and for for-each statement works with array types, so you can in fact pass an array and have it print all its values.

You may want to reconsider that example. The array is passed by reference and for for-each statement works with array types, so you can in fact pass an array and have it print all its values.

Heh, good catch. The first time I wrote it I used typename T::value_type, so arrays didn't work. My simplification broke my example smile.png

I'll fix it up...

[size=2][ I was ninja'd 71 times before I stopped counting a long time ago ] [ f.k.a. MikeTacular ] [ My Blog ] [ SWFer: Gaplessly looped MP3s in your Flash games ]

Try passing int a[10] to a function and getting its size wink.png

You mean like this?

template <class T, int N>
void printSizeOfArray(const T (&)[N])
{
    std::cout << N << std::endl;
}
It makes the array interface the same as other stl containers. It also allows passing and returning arrays by value. Arrays were a bit of a strange beast compared to other containers, but not with std::array.

Now, I would say it's better to use std::array, and leave the primitive array syntax as legacy.

Try passing int a[10] to a function and getting its size wink.png


You mean like this?

template <class T, int N>
void printSizeOfArray(const T (&)[N])
{
    std::cout << N << std::endl;
}

Exactly. I wasn't saying you couldn't, but now you've just written the ugly code yourself instead of using std::array. The ugly code has got to live somewhere. You might as well let it live outside of your own code. It's even uglier if you're passing multiple array structures.

[size=2][ I was ninja'd 71 times before I stopped counting a long time ago ] [ f.k.a. MikeTacular ] [ My Blog ] [ SWFer: Gaplessly looped MP3s in your Flash games ]
The thing about the C++ built in array type is that it has a number special case rules built into the language. For example, you can't pass an array by value to a function, use simple variable assignment to copy an array, etc. Since std::array<> is a class it obeys the normal rules rather than the special case rules, so you can do things like pass it by value. If you felt like it you could use scalar new to allocate one and scalar delete to free it, bypassing the extra book keeping overhead regular arrays usually get. And since it's a container like the other standard library containers, you can get extra debug functionality from your implementation that regular arrays may not receive.

This topic is closed to new replies.

Advertisement