len2.hpp

The following code example is taken from the book
C++ Templates - The Complete Guide, 2nd Edition
by David Vandevoorde, Nicolai M. Josuttis, and Douglas Gregor,
Addison-Wesley, 2017
© Copyright David Vandevoorde, Nicolai M. Josuttis, Douglas Gregor 2017


// number of elements in a raw array:
template<typename T, unsigned N>
std::size_t len (T(&)[N]) 

  return N;
}

// number of elements for a type having size_type:
template<typename T>
typename T::size_type len (T const& t) 

  return t.size();
}

// fallback for all other types:
std::size_t len (...)

  return 0;
}