Opened 5 years ago

Last modified 4 years ago

#7652 assigned Feature Requests

compile-time checked access

Reported by: tobias.loew Owned by: marshall
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.

Attachments (0)

Change History (7)

comment:1 Changed 5 years ago by marshall

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 5 years ago by marshall

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 5 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 5 years ago by tobias.loew

I made the last comment but forgot to login, sorry

comment:5 Changed 4 years ago by marshall

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

comment:6 Changed 4 years ago by marshall

  • Milestone changed from To Be Determined to Boost 1.54.0
  • Status changed from new to assigned

comment:7 Changed 4 years ago by marshall

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

Add Comment

Modify Ticket

Change Properties
Set your email in Preferences
as assigned The owner will remain marshall.

E-mail address and user name can be saved in the Preferences.

Note: See TracTickets for help on using tickets.