Re: array size known/not known in compile time
James Kanze:
template< typename T, size_t N >
T*
end( T (&array)[ N ] )
{
return array + N ;
}
void
f( int n )
{
int arr[ n ] ;
int* p = end( arr ) ;
}
A frequent C++ idiom. (I don't think I've written a program
which didn't use it.)
You'll love this... I've adapted it from page 219 of The C++ Standard
Library by Nicolai M. Josuttis. It makes an normal array act like a
container, without introducing any overhead whatsoever!
(I wrote the start of this post, and then spent an hour or so tweaking the
code. It's still not finished, so there may be errors or bugs, but you get
the idea. I'm going to start a new thread asking for a critique of the
code, so please post in _that_ thread if you wish to offer advice).
#define POST_IN_TERMS_OF(op) \
ThisClass operator op(int) \
{ \
ThisClass const temp(*this); \
op *this; \
return temp; \
}
#define OP_IN_TERMS_OF(op,param_dec,obj) \
ThisClass operator op(param_dec) const \
{ \
ThisClass temp(*this); \
temp op##= obj; \
return temp; \
}
#define NON_CONST_IN_TERMS_OF(ret,func_dec,call) \
ret func_dec \
{ \
return \
const_cast<ret>( \
const_cast<ThisClass const*>(this)-> call \
); \
}
#include <cstddef>
template<class T>
struct ReversePtr {
typedef ReversePtr ThisClass;
typedef std::size_t size_t;
T *p;
ReversePtr(T *const arg) : p(arg) {}
operator T*() { return p; }
operator T const *() const { return p; }
ReversePtr &operator++() { --p; return *this; }
ReversePtr &operator--() { ++p; return *this; }
POST_IN_TERMS_OF(++)
POST_IN_TERMS_OF(--)
ReversePtr &operator+=(size_t const i) { p -= i; return *this; }
ReversePtr &operator-=(size_t const i) { p += i; return *this; }
OP_IN_TERMS_OF(+,size_t const i,i)
OP_IN_TERMS_OF(-,size_t const i,i)
T const &operator[](size_t const i) const { return *(p-i); }
NON_CONST_IN_TERMS_OF(T&,operator[](size_t const i),operator[](i))
};
#include <stdexcept>
template<class T,std::size_t len>
struct IntrinsicArray {
typedef IntrinsicArray ThisClass;
T arr[len];
typedef T value_type;
typedef T *iterator;
typedef T const *const_iterator;
typedef T &reference;
typedef T const &const_reference;
typedef std::size_t size_type;
typedef std::ptrdiff_t difference_type;
T const *begin() const { return arr; }
NON_CONST_IN_TERMS_OF(T*,begin(),begin())
T const *end() const { return arr+len; }
NON_CONST_IN_TERMS_OF(T*,end(),end())
ReversePtr<T const> rbegin() const { return arr+(len-1); }
NON_CONST_IN_TERMS_OF(ReversePtr<T>,rbegin(),rbegin())
ReversePtr<T const> rend() const { return arr-1; }
NON_CONST_IN_TERMS_OF(ReversePtr<T>,rend(),rend())
T const &operator[](size_type const i) const {return arr[i];}
NON_CONST_IN_TERMS_OF(T&,operator[](size_type const i),operator[](i))
T const &at(size_type const i) const
{
if (i >= len) throw std::range_error();
return arr[i];
}
NON_CONST_IN_TERMS_OF(T&,at(size_type const i),at(i))
T const &front() const { return *arr; }
NON_CONST_IN_TERMS_OF(T&,front(),front())
T const &back() const { return arr[len-1]; }
NON_CONST_IN_TERMS_OF(T&,back(),back())
size_type size() const { return len; }
bool empty() const { return false; }
size_type capacity() const { return len; }
size_type max_size() const { return len; }
};
int main()
{
IntrinsicArray<int,64> arr;
}
--
Frederick Gotham
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]