Messages in this thread | | | From | Florian Weimer <> | Subject | Re: Expose 'array_length()' macro in <sys/param.h> | Date | Tue, 22 Sep 2020 12:01:02 +0200 |
| |
* 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 expression.
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.
Thanks, Florian -- Red Hat GmbH, https://de.redhat.com/ , Registered seat: Grasbrunn, Commercial register: Amtsgericht Muenchen, HRB 153243, Managing Directors: Charles Cachera, Brian Klemm, Laurie Krebs, Michael O'Neill
| |