|
Boost : |
From: Howard Hinnant (hinnant_at_[hidden])
Date: 2003-12-03 09:37:55
On Dec 3, 2003, at 7:31 AM, AlisdairM wrote:
> The following seems such an obvious utility, I was surprised I could
> not
> find it in the boost distribution. Could someone point me to the
> equivalent if I am missing it. Alternatively, is this worth cleaning
> up
> for a submission?
>
> Basic problem is to know the size of the following array:
>
> int[] aInts = { 1, 2, 3 );
<snip>
> I suspect there may be a way to make this a compile-time constant, but
> can't figure out the automatic type deduction yet.
Two similar "type traits" were proposed for the type traits std::tr1
lib:
http://anubis.dkuug.dk/jtc1/sc22/wg21/docs/papers/2003/n1519.htm
template <class T, unsigned I = 0> struct dimension {
static const std::size_t value =
implementation_defined;
typedef std::size_t value_type;
typedef integral_constant<value_type,value> type;
operator type()const;
};
This is a compile-time version of your "size" which has also been
generalized to pick out the ith dimension, defaulting to the 0th (or
outer) dimension as your size function extracts:
assert(dimension<int>::value == 0);
assert(dimension<int[2]>::value == 2);
assert(dimension<int[2][4]>::value == 4);
assert(dimension<int[][4]>::value == 4);
assert((dimension<int, 1>::value) == 0);
assert((dimension<int[2], 1>::value) == 0);
assert((dimension<int[2][4], 1>::value) == 4);
assert((dimension<int[][4], 1>::value) == 4);
The second traits in this area was rank (the number of dimensions):
template <class T> struct rank {
static const std::size_t value =
implementation_defined;
typedef std::size_t value_type;
typedef integral_constant<value_type,value> type;
operator type()const;
};
assert(rank<int>::value == 0);
assert(rank<int[2]>::value == 1);
assert(rank<int[][4]>::value == 2);
The reaction of the lwg at the last standards meeting to these two
"array traits" can be found here:
http://anubis.dkuug.dk/jtc1/sc22/wg21/docs/papers/2003/n1541.pdf
> The LWG wasnt sure whether this was useful; the few people who could
> use it reliably for metaprogramming would probably find it just as
> easy to write it themselves.
I disagree with this stance and it is on my to-do list to motivate
dimension and rank for the Spring meeting. As you appear to have a
need for at least "dimension", I would appreciate it if you could
expound on your desire for this functionality. Any arguments you might
have for why this functionality belongs in the std::tr1::type_traits
lib will be incorporated into my own comments and presented to the lwg.
If I'm mistaken, and no one here would like to see dimension and rank
in the std::tr1::type_traits lib, then please let me know that too. I
/believe/ these would be useful. But I do not /know/ they would. If
I'm mistaken, it wouldn't be the first time, and I might as well find
out sooner than later. :-)
-Howard
Boost list run by bdawes at acm.org, gregod at cs.rpi.edu, cpdaniel at pacbell.net, john at johnmaddock.co.uk