This is the mail archive of the
gcc@gcc.gnu.org
mailing list for the GCC project.
Re: RFC: allowing compound assignment operators with designated initializers
- From: Florian Weimer <fw at deneb dot enyo dot de>
- To: Rasmus Villemoes <rv at rasmusvillemoes dot dk>
- Cc: gcc at gcc dot gnu dot org
- Date: Sun, 14 Oct 2018 21:45:22 +0200
- Subject: Re: RFC: allowing compound assignment operators with designated initializers
- References: <b4677fd9-3287-abc7-768e-be3ce7bfcedd@rasmusvillemoes.dk>
* Rasmus Villemoes:
> This is something I've sometimes found myself wishing was supported. The
> idea being that one can say
>
> unsigned a[] = { [0] = 1, [1] = 3, [0] |= 4, ...}
>
> which would end up initializing a[0] to 5. As a somewhat realistic
> example, suppose one is trying to build a bitmap at compile time, but
> the bits to set are not really known in the sense that one can group
> those belonging to each index in a usual | expression. Something like
>
> #define _(e) [e / 8] |= 1 << (e % 8)
> const u8 error_bitmap[] = { _(EINVAL), _(ENAMETOOLONG), _(EBUSY), ... }
I think it wouldn't be too hard to extend std::bitset with more
compile-time operations to support this, if that's what you need.
> Writing a small program to generate such a table as part of the build is
> not practical in a cross-compile setting (because the constants may only
> really be known to the cross-compiler, e.g. the errno values above).
This is not a problem if you use GCC anyway because you can use inline
assembly quite reliably to extract arbitrary compile-time constants.
Search for gen-as-const-headers in the glibc sources for an example.