Sign in to follow this  
MarkS

What is the purpose of std::array?

Recommended Posts

MarkS    3502

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

Share this post


Link to post
Share on other sites
Cornstalks    7030

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!

Edited by Cornstalks

Share this post


Link to post
Share on other sites
Brother Bob    10344

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.

Share this post


Link to post
Share on other sites
Cornstalks    7030

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...

Share this post


Link to post
Share on other sites
bubu LV    1436

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;
}

Share this post


Link to post
Share on other sites
King Mir    2490
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.

Share this post


Link to post
Share on other sites
Cornstalks    7030

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.

Edited by Cornstalks

Share this post


Link to post
Share on other sites
SiCrane    11839
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.

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this