// vi:set ft=cpp: -*- Mode: C++ -*- #pragma once #include "type_traits" namespace cxx { /** * Simple encapsulation for a dynamically allocated array. * * The main purpose of this class is to support C++11 range for * for simple dynamically allocated array with static size. */ template class static_vector { private: template friend class static_vector; T *_v; IDX _l; public: typedef T value_type; typedef IDX index_type; static_vector() = default; static_vector(value_type *v, index_type length) : _v(v), _l(length) {} template::type, T>::value>::type> constexpr static_vector(Z &v) : _v(v), _l(array_size(v)) {} /// Conversion from compatible arrays template::value>::type> static_vector(static_vector const &o) : _v(o._v), _l(o._l) {} index_type size() const { return _l; } bool empty() const { return _l == 0; } value_type &operator [] (index_type idx) { return _v[idx]; } value_type const &operator [] (index_type idx) const { return _v[idx]; } value_type *begin() { return _v; } value_type *end() { return _v + _l; } value_type const *begin() const { return _v; } value_type const *end() const { return _v + _l; } value_type const *cbegin() const { return _v; } value_type const *cend() const { return _v + _l; } /// Get the index of the given element of the array index_type index(value_type const *o) const { return o - _v; } index_type index(value_type const &o) const { return &o - _v; } }; }