From 8321112502042873581
X-Google-Thread: 7894ca11fe,8390690191732735
X-Google-Attributes: gid7894ca11fe,public,usenet
X-Google-NewGroupId: yes
X-Google-Language: ENGLISH,ASCII-7-bit
Path: g2news1.google.com!news1.google.com!news.glorb.com!news.alt.net!frodo.cs.rpi.edu!not-for-mail
From: Faisal Vali <faisalv@gmail.com>
Newsgroups: comp.std.c++
Subject: Re: Why is std::array an aggregate?
Date: Tue, 22 Dec 2009 15:48:02 CST
Organization: http://groups.google.com
Lines: 106
Sender: cppmods@cs.rpi.edu
Approved: austern@google.com
Message-ID: <4b375f83-7087-46a8-a8fb-809351ad1b9d@g26g2000yqe.googlegroups.com>
References: <hgf8r0$7ta$1@news.albasani.net>
 <2fba9f88-3e5e-472f-a2fa-b7b415f07f69@g26g2000yqe.googlegroups.com>
 <e4267a16-77ab-4f65-8f02-904e3e60f201@h9g2000yqa.googlegroups.com>
 <766c67e3-b39a-474b-b48b-56f67249f78c@j4g2000yqe.googlegroups.com>
NNTP-Posting-Host: netlab.cs.rpi.edu
Content-Type: text/plain; charset=ISO-8859-1
To: (Usenet)
Return-Path: <cppmods@ruralroute.cs.rpi.edu>
X-Original-Date: Tue, 22 Dec 2009 12:39:09 -0800 (PST)
X-Submission-Address: std-c++@netlab.cs.rpi.edu
Xref: g2news1.google.com comp.std.c++:1967

On Dec 21, 12:01 pm, Yechezkel Mett <ymett.on.use...@gmail.com> wrote:
>
> > On Dec 19, 12:55 am, James Kanze <james.ka...@gmail.com> wrote:
>
> > >  (It still needs a
> > > mechanism for the number of elements to be determined,
> > > statically, from the initialization list, in order to be truly
> > > useful.)
<snip>
> How about
>
> template<class... E>
> constexpr
> std::array<std::common_type<E...>, sizeof...(E)>
> make_array(E... e)
> { return {e...}; }
>

Well, if you're willing to use the preprocessor - I believe you can
come even closer to the desired syntax with the following hack:

#define make_array(...)                                        \
         std::array<
\
            decltype(type_helper(__VA_ARGS__))  \
           , sizeof(size_helper(__VA_ARGS__))     \
         >  __VA_ARGS__;                                     \
       /**/

auto a = make_array({1,2,3,4});

And the above relies on the following two functions to work:

1)  type_helper is:
template<class T> T type_helper(std::initializer_list<T>);


2) size_helper is slightly more complicated, but is a sequence of
overloads generated
using the BOOST_PP library as follows:

#define PRINT_PARAM(z,n,param) param

#define MAKE_SIZE_HELPERS(z, n, unused) \
 struct BOOST_PP_CAT(A,n) {                            \
    template<class T>                                          \
       BOOST_PP_CAT(A,n)(                                  \
         BOOST_PP_ENUM(n, PRINT_PARAM,T&&) \
       );
         \
       typedef char (&type)[n];                                        \
       };                                                                      \

\
       BOOST_PP_CAT(A,n)::type                          \
              size_helper(BOOST_PP_CAT(A,n));  \
       /**/

  BOOST_PP_REPEAT_FROM_TO(1,150,MAKE_SIZE_HELPERS, ~)

#undef PRINT_PARAM
#undef MAKE_SIZE_HELPERS

// The above expands into something along the lines of the following:

       struct A1
       {
               template<class T> A1(T&&);
               typedef char (&type)[1];
       };
       A1::type size_helper(A1);

       struct A2
       {
               template<class T> A2(T&&,T&&);
               typedef char (&type)[2];
       };
       A2::type size_helper(A2);

      struct A3 ...



My Preprocessor metaprogramming skills are quite unseasoned, so if
there is a better way to code the above repetition - I would
appreciate any pointers in that direction.

Eitherway, while I do not have a C++0x compiler to check it against,
based on the latest draft (n3000), the above should work.

What do you guys think?

regards,
Faisal Vali
Radiation Oncology
Loyola




--
[ comp.std.c++ is moderated.  To submit articles, try just posting with ]
[ your news-reader.  If that fails, use mailto:std-c++@netlab.cs.rpi.edu]
[              --- Please see the FAQ before posting. ---               ]
[ FAQ: http://www.comeaucomputing.com/csc/faq.html                      ]



