util::array
Moderator: Forum Moderators
util::array
After a discussion with Ayin, we've decided to add a directive to the coding standards (http://wesnoth.slack.it/?CodingStandards) not to use C-style arrays in Wesnoth.
A wrapper to C-style arrays has been added in array.hpp, util::array. It should be used instead of C-style arrays at all times.
E.g.
instead of,
David
A wrapper to C-style arrays has been added in array.hpp, util::array. It should be used instead of C-style arrays at all times.
E.g.
Code: Select all
util::array<int,10> myints;
Code: Select all
int myints[10];
“At Gambling, the deadly sin is to mistake bad play for bad luck.” -- Ian Fleming
-
- Posts: 129
- Joined: April 27th, 2004, 4:24 pm
Because C-style arrays are ugly to use, and the interface is inconsistent with C++-style container classes.mlangsdorf wrote:What's the rational behind this? I'm not questioning the decision, I just don't have any idea why it was made.
E.g. to iterate over an array of integers:
Code: Select all
for(int* i = myarray; i != myarray + sizeof(myarray)/sizeof(*myarray); ++i) {
...
}
Code: Select all
for(vector<int>::iterator i = myarray.begin(); i != myarray.end(); ++i) {
...
}
Code: Select all
for(array<int,n>::iterator i = myarray.begin(); i != myarray.end(); ++i) {
...
}
David
“At Gambling, the deadly sin is to mistake bad play for bad luck.” -- Ian Fleming
What's the reasoning behind adding an own wrapper? It's not as if std::vector<> would be more then a quite sophisticated wrapper. I doubt that you can implement any of the wrapper functions more efficient then std::vector<> because as part of the c++ library, it'll most likely use compiler specific optimizations normal user won't ever know of.
std::vector is less efficient because it allocates memory dynamically (on the free store), while a C-style array allocates it on the stack, which is extremely fast.torangan wrote:What's the reasoning behind adding an own wrapper? It's not as if std::vector<> would be more then a quite sophisticated wrapper. I doubt that you can implement any of the wrapper functions more efficient then std::vector<> because as part of the c++ library, it'll most likely use compiler specific optimizations normal user won't ever know of.
The util::array I added is a very very thin wrapper over a C-style array. It's similiar to the wrapper in Boost. I just wrote my own because it was about as simple to write it myself as it was to extract the code from Boost while getting rid of the Boost header dependencies and weird code to handle strange compilers.
David
“At Gambling, the deadly sin is to mistake bad play for bad luck.” -- Ian Fleming
Huh? My entire first post in this thread said exactly that.torangan wrote:Well sure, I just didn't think about small, stack allocated arrays. You should have written that it's intended to be used for stack allocated arrays and std::vector for heap allocated arrays. Or I should have thought a moment longer, either would've done it.
David
“At Gambling, the deadly sin is to mistake bad play for bad luck.” -- Ian Fleming
It's not legal syntax in C++. In C++ you go,torangan wrote:Well it's legal syntax in C++ as well, but yes, I've been forced to program in Java for more then a year now. I do like C++ way more but the decision is made by those who pay the money I want. When I'm coding open source, it's always C++ though.
int* foo = new int[...];
or you can go,
int foo[] = {...};
but never int[] foo
David
“At Gambling, the deadly sin is to mistake bad play for bad luck.” -- Ian Fleming