Boost logo

Boost :

Subject: [boost] [array] change proposal for the c_array() function
From: herve martin (hervemart1_at_[hidden])
Date: 2009-03-14 05:28:39


Hi,

  I'm new in this mailing list .My interest are embedded software and code
reuse.

I would submit a simple change request in the boost::array library.
The boost::c_array() function is expected to cast a boost::array<T,N> into a
C-array but actually the returned type is a T* which is not exactly a
reference on a C-array.

to keep track of the static size and make sizeof( myarray.c_array() )
returning the real size of the C-array,
I suggest to change:
T* c_array() { return elems; }

into
T (&c_array())[N] { return *(T(*)[N]) elems; }

by the way,
I have developed a function helper array_cast<> whose purpose is to:
 * make a bit more friendly the syntax when casting to a C-array
 * check consistency between size of the input type and size of the target
C-array

for example,
{
    long titi;
    char (&test)[4] = utils::array_cast<char>( titi );

    test[0] = 0;
}
assuming that long is 32 bit long and char is 8 bit long,
'test[0]' is now a reference on the 1st byte of 'titi'. Moreover declaring
test as a char(&)[5] would have failed in compiling since size of both types
are not suitable (but the function provides some other way to force cast
when sizes are not compatible)

do you think that there could be any interest to include this function in
boost library ?

thanks,

herve


Boost list run by bdawes at acm.org, gregod at cs.rpi.edu, cpdaniel at pacbell.net, john at johnmaddock.co.uk