[lkml]   [2020]   [Sep]   [22]   [last100]   RSS Feed
Views: [wrap][no wrap]   [headers]  [forward] 
Messages in this thread
SubjectRe: Expose 'array_length()' macro in <sys/param.h>
* Jonathan Wakely:

> I don't see much point in using std::size here. If you're going to
> provide the alternative implementation for when std::size isn't
> defined, why not just use it always?
> template<typename _Tp, std::size_t _Num>
> #if __cplusplus >= 201103L
> constexpr
> #endif
> inline std::size_t
> __array_length(const _Tp(&)[_Num]) __THROW
> {
> return _Num;
> }
> This only requires <cstddef>, not <iterator>.

I agree that this is an advantage. But the version without constexpr is
not sufficient because __array_length does not produce a constant

I've seen something like this used instead:

template<typename _Tp, std::size_t _Num>
char (&___array_length(const _Tp(&)[_Num]))[_Num];
#define __array_length(v) (sizeof(___array_length(v)))

If the function type is too cute, a helper struct could be used instead.

Red Hat GmbH, , Registered seat: Grasbrunn,
Commercial register: Amtsgericht Muenchen, HRB 153243,
Managing Directors: Charles Cachera, Brian Klemm, Laurie Krebs, Michael O'Neill

 \ /
  Last update: 2020-09-22 12:02    [W:0.040 / U:3.004 seconds]
©2003-2020 Jasper Spaans|hosted at Digital Ocean and TransIP|Read the blog|Advertise on this site