Jump to content

  • Log In with Google      Sign In   
  • Create Account

We're offering banner ads on our site from just $5!

1. Details HERE. 2. GDNet+ Subscriptions HERE. 3. Ad upload HERE.


What is the purpose of std::array?


Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.

  • You cannot reply to this topic
7 replies to this topic

#1 MarkS   Prime Members   -  Reputation: 886

Like
0Likes
Like

Posted 25 February 2013 - 03:39 PM

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



Sponsor:

#2 Cornstalks   Crossbones+   -  Reputation: 6991

Like
3Likes
Like

Posted 25 February 2013 - 03:50 PM

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, 25 February 2013 - 06:45 PM.

[ 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 ]

#3 Brother Bob   Moderators   -  Reputation: 8571

Like
3Likes
Like

Posted 25 February 2013 - 04:04 PM

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.



#4 Cornstalks   Crossbones+   -  Reputation: 6991

Like
0Likes
Like

Posted 25 February 2013 - 04:09 PM

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


[ 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 ]

#5 Martins Mozeiko   Crossbones+   -  Reputation: 1422

Like
1Likes
Like

Posted 25 February 2013 - 04:18 PM

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


#6 King Mir   Members   -  Reputation: 2050

Like
0Likes
Like

Posted 25 February 2013 - 04:25 PM

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.

#7 Cornstalks   Crossbones+   -  Reputation: 6991

Like
1Likes
Like

Posted 25 February 2013 - 05:28 PM

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, 25 February 2013 - 05:30 PM.

[ 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 ]

#8 SiCrane   Moderators   -  Reputation: 9662

Like
3Likes
Like

Posted 25 February 2013 - 06:40 PM

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.




Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.



PARTNERS