Opened 6 years ago

Last modified 6 years ago

#7652 assigned Feature Requests

compile-time checked access

Reported by: Tobias Loew Owned by: Marshall Clow
Milestone: Boost 1.54.0 Component: array
Version: Boost 1.52.0 Severity: Optimization
Keywords: Cc:


Since code like

boost::array<int,2> test; test[2] = 1; test[-1] = 1;

compiles correctly on some compilers (even without warnings). I suggest adding compile checked functions for static access to arrays like:

template<size_type i> reference at() {

BOOST_STATIC_ASSERT( (i < N) ); return elems[i];


template<size_type i> const_reference at() const {

BOOST_STATIC_ASSERT( (i < N) ); return elems[i];


then code like:

boost::array<int,2> test;<2> = 1;<-1> = 1;

would result in the expected errors.

Change History (7)

comment:1 Changed 6 years ago by Marshall Clow

Yes, it would - but would people actually use it?

Do people use:

std::array<int, 2> arr;
std::get<2>(arr) = -1;

in C++11 - or do they just write arr[2] = -1; ?

boost::array (and std::array, and std::vector) all have runtime checking through the member function at.

boost::array<int, 2> arr; = -1;    // will throw an exception

comment:2 Changed 6 years ago by Marshall Clow

In [82083], I added support for std::get<> to Boost.Array, so you can write

boost::array<int, 2> arr;
std::get<2>(arr) = -1;

and get a compile-time error.

Right now, it's only turned on for C++11, but I might be able to make it work for C++03 systems.

comment:3 Changed 6 years ago by anonymous

The stl-version of VS 2008 has the same code for "get" and std::tr1::array<T,N> and it compiles fine with C++03, so it works at least for that compiler.

comment:4 Changed 6 years ago by Tobias Loew

I made the last comment but forgot to login, sorry

comment:5 Changed 6 years ago by Marshall Clow

I also added overloads of boost::get for use on C++03

comment:6 Changed 6 years ago by Marshall Clow

Milestone: To Be DeterminedBoost 1.54.0
Status: newassigned

comment:7 Changed 6 years ago by Marshall Clow

Also, in [82834], I marked a bunch of methods of Boost.Array as "constexpr".

Note: See TracTickets for help on using tickets.