Marcin
Marcin

Reputation: 422

Differences between std::array and boost::array

It is fully safe to change boost::array to std::array? Can it cause any discrepancies?

Is boost::array better over std::array (performance?)?

Upvotes: 6

Views: 3307

Answers (1)

Yakk - Adam Nevraumont
Yakk - Adam Nevraumont

Reputation: 275310

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.

Upvotes: 7

Related Questions