Boost logo

Boost :

Subject: Re: [boost] [util] Any interest in address_of_first_nonarray_elem ?
From: Daryle Walker (darylew_at_[hidden])
Date: 2013-12-04 11:14:33


> From: mostafa_working_away_at_[hidden]
> Date: Sun, 1 Dec 2013 20:10:53 -0800
>
> 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?

I needed code like this when developing a multiple-dimension extension to "array." What about (not tested; quasi-C++14):

//=====
template < typename T>
constexpr
auto inner_addressof( T &&t, false_type )
{ return addressof(forward<T>(t)); }

template < typename T, size_t N>
constexpr
auto inner_addressof( T (&t)[N], true_type )
{ return inner_addressof(t[0], integral_constant<bool, rank<T>::value>{}); }

template < typename T>
constexpr
auto inner_addressof( T &&t )
{ return inner_addressof(forward<T>(t), integral_constant<bool, rank<T>::value>{}); }

template < typename T>
struct inner_element_count
    : integral_constant<size_t, sizeof(T) / sizeof(remove_all_extents_t<T>)>
{ };
//=====

No need for a custom range class.

Daryle W.


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