Opened 20 years ago

Closed 14 years ago

#87 closed Feature Requests (fixed)

function to get the size of a array

Reported by: flier Owned by: mark_rodgers
Milestone: Component: functional
Version: None Severity: Showstopper
Keywords: Cc:

Description

why not add a function which can get the size of a 
array? just like this

#include <boost/type_traits.hpp>

template <bool IS_CLASS>
struct SizeOfArrayHelper;

template <>
struct SizeOfArrayHelper<false>
{
  template<typename T>
  static inline int SizeOfArray(const T& array)
  {
    BOOST_STATIC_ASSERT(boost::is_array<T>::value);

    return sizeof(array) / sizeof(array[0]);
  }

  static inline int SizeOfArray(char *str)
  {
    return strlen(str);
  }

  static inline int SizeOfArray(const char *str)
  {
    return strlen(str);
  }
};

template <>
struct SizeOfArrayHelper<true>
{
  template<typename T>
  static inline int SizeOfArray(const T& array)
  {
    BOOST_STATIC_ASSERT(boost::is_class<T>::value);

    return array.size();
  }
};

template <typename T>
inline int SizeOfArray(const T& array)
{
  return 
SizeOfArrayHelper<boost::is_class<T>::value>::SizeOfArr
ay(array);
}

Change History (2)

comment:1 by nobody, 20 years ago

Logged In: NO 

-----------------
what's wrong with boost::array<>?

Thorsten Ottosen, nesotto@cs.auc.dk

comment:2 by Steven Watanabe, 14 years ago

Resolution: Nonefixed
Severity: Showstopper
Status: assignedclosed

The Range library provides boost::size.

http://www.boost.org/libs/range/doc/boost_range.html#size

Note: See TracTickets for help on using tickets.