Baruch
Baruch

Reputation: 21538

STL container requierments

Does the standard require that some_container<T>::value_type be T?

I am asking because I am considering different approaches to implementing an STL-compliant 2d dynamic array. One of them is to have 2Darray<T>::value_type be 2Darray_row<T> or something like that, where the array would be iterated as a collection of rows (a little simplified. My actual implementation allows iteration in 3 directions)

Upvotes: 4

Views: 182

Answers (3)

MWid
MWid

Reputation: 4579

Table 96 in §23.2.1 in the standard (c++11) requires a container class X containing objects of type T to return T for X::value_type.

So, if your some_container stores objects of type T, then value_type has to be T.

Upvotes: 2

Dietmar K&#252;hl
Dietmar K&#252;hl

Reputation: 154025

The container requirements are a bit funky in the sense that they are actually not used by any generic algorithm. In that sense, it doesn't really matter much.

That said, the requirements are on the interface for containers not on how the container is actually instantiated. Even non-template classes can conform to the various requirements and, in fact, do. The requirement is that value_type is present; what it is defined to depends entirely on the container implementation.

Upvotes: 5

Phil H
Phil H

Reputation: 20151

Either have a nested container (so colArray<rowArray<T> >) or have a single wrapping (2dArray<T>), but don't try to mix them. The nested approach allows you to use STL all the way down (vector<vector<T> >), but can be confusing and doesn't allow you column iterators etc, which you seem to want.

This SO answer addresses using ublas, and another suggests using Boost multi-arrays.

Generally, go for the STL or Boost option if you can. You are unlikely to write something as well by yourself.

Upvotes: 0

Related Questions