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
}
If your array is fixed-size, just return the size (n
in your example), since that is also the maximum size.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With