public inbox for libc-alpha@sourceware.org
 help / color / mirror / Atom feed
From: Alejandro Colomar <colomar.6.4.3@gmail.com>
To: Jonathan Wakely <jwakely@redhat.com>,
	Florian Weimer <fweimer@redhat.com>
Cc: gcc@gcc.gnu.org, libstdc++@gcc.gnu.org,
	Libc-alpha <libc-alpha@sourceware.org>,
	libc-coord@lists.openwall.com
Subject: Re: Expose 'array_length()' macro in <sys/param.h>
Date: Mon, 21 Sep 2020 23:52:13 +0200	[thread overview]
Message-ID: <e734429a-d543-7e75-48e9-a8297a94b035@gmail.com> (raw)
In-Reply-To: <20200921140100.GA449323@redhat.com>

I have developed this draft code, the C++ part being based on what you 
wrote.

I am a C programmer, and my C++ is very basic, and I tend to write 
C-compatible code when I need C++, so I can't really write the C++ part.

I tested the code with all C versions (--std= {c89, c99, c11, c18, 
c2x}), and it worked for all of them (correctly returning 18 in all of 
them), and if I uncomment the part of the pointer, it has a nice error 
message.  I used `-Wall -Wextra -Werror -pedantic -Wno-vla 
-Wno-sizeof-pointer-div`.

However, the C++ part needs some work to be able to compile.

Would you mind finishing it?


Thanks,

Alex
------------------------------------------------------------------

#if defined(__cplusplus)
# include <cstddef>

# if __cplusplus >= 201703L
#  include <array>
#  define array_length(arr)	(std:size(arr))
# else

#  if __cplusplus >= 201103L
constexpr
#  endif
inline std::size_t
array_length(const T(&array)[N])
#  if __cplusplus >= 201103L
noexcept
#  endif
{
	return	N;
}
# endif

# if __cplusplus >= 202002L
#  define array_slength(arr)	(std:ssize(arr))
# else

#  if __cplusplus >= 201103L
constexpr
#  endif
inline std::ptrdiff_t
array_slength(const T(&array)[N])
#  if __cplusplus >= 201103L
noexcept
#  endif
{
	return	N;
}
# endif


#else /* !defined(__cplusplus) */
#include <stddef.h>

# define __is_same_type(a, b)						\
	__builtin_types_compatible_p(__typeof__(a), __typeof__(b))
# define __is_array(arr)	(!__is_same_type((arr), &(arr)[0]))

# if __STDC_VERSION__ >= 201112L
#  define __must_be(e, msg)	(					\
	0 * (int)sizeof(						\
		struct {						\
			_Static_assert((e), msg);			\
			char ISO_C_forbids_a_struct_with_no_members__;	\
		}							\
	)								\
)
# else
#  define __must_be(e, msg)	(					\
	0 * (int)sizeof(						\
		struct {						\
			int	: (-!(e));				\
			char ISO_C_forbids_a_struct_with_no_members__;	\
		}							\
	)								\
)
# endif

# define __must_be_array(arr)	__must_be(__is_array(arr), "Must be an 
array!")

# define __array_length(arr)	(sizeof(arr) / sizeof((arr)[0]))
# define array_length(arr)	(__array_length(arr) + __must_be_array(arr))
# define array_slength(arr)	((ptrdiff_t)array_length(arr))
#endif


int main(void)
{
	int a[5];
	const int x = 6;
	int b[x];
#if __cplusplus >= 201103L
	constexpr
#endif
int y = 7;
	int c[y];
	int *p;
	(void)p;

	return	array_slength(a) + array_slength(b) +
		array_length(c) /*+
		array_length(p)*/;
}




  reply	other threads:[~2020-09-21 21:52 UTC|newest]

Thread overview: 30+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2020-09-20 23:00 Expose 'array_length()' macro in <sys/cdefs.h> or <sys/param.h> Alejandro Colomar
2020-09-21  8:38 ` Florian Weimer
2020-09-21 10:11   ` Alejandro Colomar
2020-09-21 10:33     ` Florian Weimer
2020-09-21 12:47       ` Alejandro Colomar
2020-09-21 15:47         ` [libc-coord] " enh
2020-09-22 16:25         ` Rich Felker
2020-09-22 16:44           ` Jonathan Wakely
2020-09-22 16:53             ` Ville Voutilainen
2020-09-21 14:01       ` Jonathan Wakely
2020-09-21 21:52         ` Alejandro Colomar [this message]
2020-09-21 22:04           ` Expose 'array_length()' macro in <sys/param.h> Jonathan Wakely
2020-09-21 22:13             ` Ville Voutilainen
2020-09-22  9:10               ` Alejandro Colomar
2020-09-22  9:40                 ` Jonathan Wakely
2020-09-22 10:01                   ` Florian Weimer
2020-09-22 10:35                     ` Alejandro Colomar
2020-09-22 11:40               ` Florian Weimer
2020-09-22 14:58                 ` [RFC] <sys/param.h>: Add nitems() and snitems() macros Alejandro Colomar
2020-09-25 13:20                   ` [PATCH v2] " Alejandro Colomar
2020-09-25 14:10                     ` Alejandro Colomar
2020-09-25 14:48                       ` Jonathan Wakely
2020-09-25 16:30                         ` Alejandro Colomar
2020-09-25 17:39                           ` Jonathan Wakely
2020-09-25 17:42                           ` Jonathan Wakely
2020-09-25 17:46                             ` Alejandro Colomar
2020-09-25 19:37                               ` [PATCH v3] <sys/param.h>: Add nitems() Alejandro Colomar
2020-09-22  9:16             ` [libc-coord] Re: Expose 'array_length()' macro in <sys/param.h> Florian Weimer
2020-09-30 15:58 ` Expose 'array_length()' macro in <sys/cdefs.h> or <sys/param.h> Joseph Myers
2020-09-30 20:39   ` Alejandro Colomar

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=e734429a-d543-7e75-48e9-a8297a94b035@gmail.com \
    --to=colomar.6.4.3@gmail.com \
    --cc=fweimer@redhat.com \
    --cc=gcc@gcc.gnu.org \
    --cc=jwakely@redhat.com \
    --cc=libc-alpha@sourceware.org \
    --cc=libc-coord@lists.openwall.com \
    --cc=libstdc++@gcc.gnu.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).