gexicide
gexicide

Reputation: 40128

How should I implement max_size in a constant length STL-style data structure?

Suppose I implement a constant length data structure comparable to a dynamic array. I.e., I give the data structure a length l in the constructor. Then, that instance of the data structure will never be able to hold more elements than l. I want that data structure to have an interface that is as close to STL as it can get.

How should I implement the max_size method for this class? Should it be the capacity l given in the constructor? Or should it be std::numeric_limits<size_type>::max()?

The documentation for this method says:

Returns the maximum number of elements the container is able to hold due to system or library implementation limitations, i.e. std::distance(begin(), end()) for the largest container.

This documentation reads as if it is for the largest container, so it should be the latter definition. However, this method is a non-static method which hints at that it should return information about the current instance of the data structure, not a general limit for how big another instance of this data structure could get.

So what is the desired semantics of max_size? Max of this instance or max of a hypothetical "largest" instance?

Upvotes: 4

Views: 258

Answers (1)

Rakete1111
Rakete1111

Reputation: 49028

Your container reminds me of some sort of dynamic std::array.

std::array::max_size() has the semantics of your former definition (the maximum size of the current instance):

Because each std::array<T, N> is a fixed-size container, the value returned by max_size equals N (which is also the value returned by size).

Upvotes: 3

Related Questions