Modify

Ticket #7652 (assigned Feature Requests)

Opened 18 months ago

Last modified 14 months ago

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:

Description

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; test.at<2> = 1; test.at<-1> = 1;

would result in the expected errors.

Attachments

Change History

comment:1 Changed 16 months 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;
arr.at(2) = -1;    // will throw an exception

comment:2 Changed 16 months 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 16 months 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 16 months ago by tobias.loew

I made the last comment but forgot to login, sorry

comment:5 Changed 16 months ago by marshall

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

comment:6 Changed 16 months ago by marshall

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

comment:7 Changed 14 months ago by marshall

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

View

Add a comment

Modify Ticket

Change Properties
<Author field>
Action
as assigned
Author


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

 
Note: See TracTickets for help on using tickets.