Boost logo

Boost :

Subject: Re: [boost] [util] Any interest in address_of_first_nonarray_elem ?
From: Mostafa (mostafa_working_away_at_[hidden])
Date: 2013-12-02 05:21:19


On Sun, 01 Dec 2013 23:56:41 -0800, Joaquin M Lopez Munoz <joaquin_at_[hidden]>
wrote:

> Mostafa <mostafa_working_away <at> yahoo.com> writes:
>
>>
>> Working with built-in array objects I find myself repeatedly needing to
>> get the address of the first non-array element. This is so I can do
>> generic array comparisons and copies without resorting to multiple
>> loops.
>> For example:
>>
>> template <typename T, std::size_t N>
>> bool generic_array_equal(T (&lhs)[N], T const (&rhs)[N])
>> {
>> return std::equal(
>> address_of_first_nonarray_elem(lhs),
>> address_of_first_nonarray_elem(lhs) + total_extent<T[N]>::size,
>> address_of_first_nonarray_elem(rhs));
>> }
>>
>> Any interest in adding such a function to the util library?
>
> Why the "non"? Shouldn't the name be "address_of_first_array_elem"?
> How is this different from std::begin(lhs)?

No. The motivating use case for this were built-in multidimensional
arrays. I wanted a generic way to compare and copy them without resorting
to nested loops.

For example:

   typedef int ArrType[2][3];

   ArrType a1 = { {0, 1, 2}, {3, 4, 5} };
   ArrType a2 = { {0, 1, 2}, {3, 4, 5} };

   // Prints 0.
   std::cout << "Std Equal: " << std::equal(a1, a1 + 2, a2) << "\n";
   // Prints 1. Obviously address_of_first_nonarray_elem would be used
instead of subscripting.
   std::cout
     << "Flattened Std Equal: " << std::equal(a1[0], a1[0] + 6, a2[0]) <<
"\n";

In the above example the address of the first array element won't do,
since that itself is an array. In general what is needed is the address of
the first non-array element, and in the above case that would be &a1[0][0]
or a1[0].


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