It is fully safe to change boost::array
to std::array
? Can it cause any discrepancies?
Is boost::array
better over std::array
(performance?)?
std::array is just a class version of the classic C array. That means its size is fixed at compile time and it will be allocated as a single chunk (e.g. taking space on the stack). The advantage it has is slightly better performance because there is no indirection between the object and the arrayed data.
It will provide a value like semantics equally to the other C++ containers. A std::array should have same runtime performance as a c-style array.
std::array provides many benefits over built-in arrays, such as preventing automatic decay into a pointer, maintaining the array size, providing bounds checking, and allowing the use of C++ container operations.
So in conclusion: yes, std::array is on the stack.
std::array<T,N>
and boost::array<T,N>
are standard layout aggregates containing nothing but an array of T[N]
.
Their interaction with namespace boost
and namespace std
may be different (Specifically, ADL will find std
functions for std::array
, and boost
functions for boost::array
).
So, if there is a function foo
in boost
, calling foo(some_array)
might work if some_array
was from boost
, and not if it was from std
.
The only container algorithms currently in std
are std::begin
and std::end
(and similar the new ones size
empty
etc if you include near-future ones). Ranges v3 (or whatever gets published) might add some more.
There are more container algorithms in boost
than in std
. Some of them might fail to build.
In the worst case, someone could write a function with the same name as a container algorithm in boost
, with an argument that can implicitly convert from std::array
, and the unqualified call to that function could result in a conversion after you change the variable type, while before it called the boost
container algorithm.
Similarly, someone could write code that explicitly checks if a template argument is a boost::array
and behave differently if it is.
Both of those are a bit of a stretch.
Finally, std::array
has modern noexcept
decoration, and boost has a public c_array
member you can get at (the name std::array
member variables is, I believe, not specified). (via @Potatoswatter). I'd personally expect std::array
to have better support going forward, as boost::array
mainly existed because std
lacked the functionality.
Other than those corner cases, std::array
should be a drop-in replacement.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With