[cfe-dev] warning request: partial array initialisation
Matthieu Monrocq
matthieu.monrocq at gmail.com
Wed Jun 20 09:29:58 PDT 2012
On Wed, Jun 20, 2012 at 12:53 AM, Richard Smith <richard at metafoo.co.uk>wrote:
> On Tue, Jun 19, 2012 at 3:17 PM, Sebastian Redl
> <sebastian.redl at getdesigned.at> wrote:
> >
> > On 19.06.2012, at 18:49, Chandler Carruth wrote:
> >
> > On Tue, Jun 19, 2012 at 1:20 AM, Jay Foad <jay.foad at gmail.com> wrote:
> >>
> >> Hi,
> >>
> >> I'd really like to be able to get a warning when "almost all" elements
> >> of an array are initialised:
> >>
> >> #define N 8
> >> const int a[N] = { 3, 1, 4, 1, 5, 9, 2 };
> >> warning: some but not all array elements initialised
> >
> >
> > At least in C++, this warning wouldn't make a lot of sense because all of
> > the array elements *are* initialized. [dcl.init.aggr] 8.5.1/7: "If there
> are
> > fewer initializers in the list than there are members in the aggregate,
> then
> > each member not explicitly initialized shall be value-initialized (8.5)."
> >
> > We would have a problem with lots of code which intended to initialized
> the
> > first few elements, and zero out the rest.
> >
> > Maybe someone has clever ideas about how to syntactically differentiate
> > between these two use cases?
> >
> >
> > const int a[] = { 3, 1, 4, 1, 5, 9, 2 };
> > static_assert(arraysize(a) == N, "N was increased, you need more
> > initializers");
>
> Ooh, can I play?
>
> // In a library...
> struct end_t {} constexpr end {};
> template<typename T> struct WithEnd { T a; end_t end; };
>
> // error, no conversion from end_t to int
> WithEnd<const int[N]> a = { 3, 1, 4, 1, 5, 9, 2, end };
>
> ... or ...
>
> // In a library...
> template<typename T, size_t...Ns> struct ArrImpl {
> constexpr ArrImpl(const decltype(Ns, declval<T>()) &...arg) : arr{arg...}
> {}
> T arr[sizeof...(Ns)];
> };
> template<typename T> struct ArrBuilder;
> template<typename T, size_t N> struct ArrBuilder<T[N]> {
> typedef typename ArrBuilder<ArrImpl<T,N-1>>::type type;
> };
> template<typename T, size_t ...Ns> struct ArrBuilder<ArrImpl<T, 0, Ns...>>
> {
> typedef ArrImpl<T, 0, Ns...> type;
> };
> template<typename T, size_t N, size_t ...Ns> struct
> ArrBuilder<ArrImpl<T, N, Ns...>> {
> typedef typename ArrBuilder<ArrImpl<T, N-1, N, Ns...>>::type type;
> };
> template<typename T> using Arr = typename ArrBuilder<T>::type;
>
> // error, no matching constructor
> Arr<const int[8]> a = { 1, 2, 3, 4, 5, 6, 7 };
>
> _______________________________________________
> cfe-dev mailing list
> cfe-dev at cs.uiuc.edu
> http://lists.cs.uiuc.edu/mailman/listinfo/cfe-dev
>
I think Sebastian got the easiest alternative!
-- Matthieu
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.llvm.org/pipermail/cfe-dev/attachments/20120620/818adaae/attachment.html>
More information about the cfe-dev
mailing list