Boost logo

Boost :

Subject: Re: [boost] [array] change proposal for the c_array() function
From: Niels Dekker - mail address until 2010-10-10 (niels_address_until_2010-10-10_at_[hidden])
Date: 2009-03-14 10:56:38

herve martin wrote:
> I would submit a simple change request in the boost::array library.
> I suggest to change:
> T* c_array() { return elems; }
> into
> T (&c_array())[N] { return *(T(*)[N]) elems; }

Believe it or not, I was considering to submit the very same feature
request. But go ahead:
(Please consider requesting a const overload as well!)

Actually I did the very same request for std::array! But apparently the
C++ Library Working Group considers a different resolution, adding
explicit conversion operators instead, converting an std::array to a
reference to its built-in array:

BTW, Nicolai Josuttis, the author of boost::array, told me that
c_array() is only there for historical reasons.

> 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)

I'm interested to know if such an array_cast function could be made
platform independent and safe with respect to byte alignment.

Kind regards, Niels

Niels Dekker
Scientific programmer at LKEB, Leiden University Medical Center

Boost list run by bdawes at, gregod at, cpdaniel at, john at