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 , 10 years ago
comment:2 by , 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 , 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:6 by , 10 years ago
| Milestone: | To Be Determined → Boost 1.54.0 |
|---|---|
| Status: | new → assigned |
comment:7 by , 10 years ago
Also, in [82834], I marked a bunch of methods of Boost.Array as "constexpr".

Yes, it would - but would people actually use it?
Do people use:
in C++11 - or do they just write
arr[2] = -1;?boost::array(andstd::array, andstd::vector) all have runtime checking through the member functionat.