Opened 10 years ago

Last modified 10 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:

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.

Change History (7)

comment:1 by Marshall Clow, 10 years ago

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 by Marshall Clow, 10 years ago

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 by anonymous, 10 years ago

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 by Tobias Loew, 10 years ago

I made the last comment but forgot to login, sorry

comment:5 by Marshall Clow, 10 years ago

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

comment:6 by Marshall Clow, 10 years ago

Milestone: To Be DeterminedBoost 1.54.0
Status: newassigned

comment:7 by Marshall Clow, 10 years ago

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

Note: See TracTickets for help on using tickets.