Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I implement array::max_size()?

I am building my own array<T, n> class template for fun and education. The C++0x standard draft specifies a member function max_size() for all containers as distance(begin(), end()) "for the largest possible container". How do I implement this member function for arrays? Do I simply return std::numeric_limits<std::size_t>::max(), or should the result depend on the element type?


Hmm, both std::array from current g++ and boost::array return n from max_size():

#include <array>
#include <boost/array.hpp>
#include <iostream>

int main()
{
    std::array<int, 11> foo;
    std::cout << foo.max_size() << std::endl;   // prints 11

    boost::array<int, 11> bar;
    std::cout << bar.max_size() << std::endl;   // prints 11
}
like image 515
fredoverflow Avatar asked Dec 04 '22 09:12

fredoverflow


1 Answers

If your array is fixed-size, just return the size (n in your example), since that is also the maximum size.

like image 181
Jeremiah Willcock Avatar answered Dec 21 '22 22:12

Jeremiah Willcock