Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

const boost::array<T,N> or boost::array<const T,N>?

What is difference between these two? Which one you would prefer when you need a fixed size array of constant values?

const boost::array<int, 2> x = {0, 1};
boost::array<const int, 2> y = {0, 1};

Thanks.

like image 956
pic11 Avatar asked Apr 09 '11 09:04

pic11


1 Answers

The second one will prevent that you copy it to a new non-const array

boost::array<const int, 2> y = {0, 1};
boost::array<int, 2> y1 = y; // error!

Since I would expect that to work, I would probably go with the first option. Passing the second one to templates that expect a boost::array<T, N> will prevent those templates from modifying their parameter (even if it's a copy). The first one would "just work", since the parameter would have the type boost::array<int, 2>.

like image 114
Johannes Schaub - litb Avatar answered Sep 17 '22 01:09

Johannes Schaub - litb