220 18346 <fe7fcb9b-fbcb-4437-953d-f4bfd807e4e0@isocpp.org> article
Path: news.gmane.org!not-for-mail
From: Matthew Fioravante <fmatthew5876@gmail.com>
Newsgroups: gmane.comp.lang.c++.isocpp.proposals
Subject: Re: Use cases for dyn_array
Date: Mon, 1 Jun 2015 16:34:32 -0700 (PDT)
Lines: 235
Approved: news@gmane.org
Message-ID: <fe7fcb9b-fbcb-4437-953d-f4bfd807e4e0@isocpp.org>
References: <a0542458-0240-4644-b313-55e257823db1@isocpp.org>
 <CAFk2RUbREdNuGDaZugxDwG=Ck66LhC4knWTTR-mLGvGktfP5Rg@mail.gmail.com>
 <c6596fb4-ec4b-4263-b60f-ded11e4b9af0@isocpp.org>
 <F2E7E68D-491E-42AB-87A8-975FD55897B7@mac.com>
 <064e1cec-7a5b-4550-b694-6570e1702776@isocpp.org>
 <a28f8a4c-fee9-424d-b30a-a47e51d286ab@isocpp.org>
 <7AC283E4-961E-4AC3-8EDF-AB274DC02BCB@mac.com>
 <955aafda-3636-4881-9e83-ff07240dd0d7@isocpp.org>
 <0af21d5d-e95e-41c9-8963-69be0cfbf366@isocpp.org>
Reply-To: std-proposals@isocpp.org
NNTP-Posting-Host: plane.gmane.org
Mime-Version: 1.0
Content-Type: multipart/mixed; 
	boundary="----=_Part_4217_177766264.1433201672247"
X-Trace: ger.gmane.org 1433201676 17046 80.91.229.3 (1 Jun 2015 23:34:36 GMT)
X-Complaints-To: usenet@ger.gmane.org
NNTP-Posting-Date: Mon, 1 Jun 2015 23:34:36 +0000 (UTC)
Cc: potswa@mac.com
To: std-proposals@isocpp.org
Original-X-From: std-proposals+bncBDELF54RTIGRBCGYWOVQKGQE4DTU73A@isocpp.org Tue Jun 02 01:34:36 2015
Return-path: <std-proposals+bncBDELF54RTIGRBCGYWOVQKGQE4DTU73A@isocpp.org>
Envelope-to: gclcip-std-proposals@m.gmane.org
Original-Received: from mail-oi0-f70.google.com ([209.85.218.70])
	by plane.gmane.org with esmtp (Exim 4.69)
	(envelope-from <std-proposals+bncBDELF54RTIGRBCGYWOVQKGQE4DTU73A@isocpp.org>)
	id 1YzZE2-000476-IV
	for gclcip-std-proposals@m.gmane.org; Tue, 02 Jun 2015 01:34:34 +0200
Original-Received: by oihb142 with SMTP id b142sf239706113oih.2
        for <gclcip-std-proposals@m.gmane.org>; Mon, 01 Jun 2015 16:34:33 -0700 (PDT)
DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed;
        d=gmail.com; s=20120113;
        h=date:from:to:cc:message-id:in-reply-to:references:subject
         :mime-version:content-type:x-original-sender:reply-to:precedence
         :mailing-list:list-id:list-post:list-help:list-archive
         :list-subscribe:list-unsubscribe;
        bh=20Zk4xKTtFe9Bo7RhMQA/1e4bl+Fuie4+wN/McJ0048=;
        b=Y0BoZ1HHIHEbqlact5vQfCzAYgeA6/DZ00yUc53SJz1UpwdqsPVWKKL+U/h3W22Uj2
         WFp4DK3Jau2mM528Ro2CzccQ70PT0YAajvGG1clzC4ZfQ+Hd8tt3MS7yccm38HbKHk9s
         nrbxEZV1xdquRS2dKVKmcQJYwVWHfmN+1NkmXqTcT55Rg9S1nKOj6acQcFExfFEnwucG
         qETFFijQ7iKBBoqspdJdhxrpRL3jNvLutahpCsX8+Njs7WOeYewKwu/LcCly0VylLjr+
         5jHa63sotO8d/dlgbhbQcd5baPbcl/xtZA/2igaOx6yBtZ11LiJAE4ZgJ7Txesh34NtO
         4HdQ==
X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed;
        d=1e100.net; s=20130820;
        h=x-gm-message-state:date:from:to:cc:message-id:in-reply-to
         :references:subject:mime-version:content-type:x-original-sender
         :reply-to:precedence:mailing-list:list-id:list-post:list-help
         :list-archive:list-subscribe:list-unsubscribe;
        bh=20Zk4xKTtFe9Bo7RhMQA/1e4bl+Fuie4+wN/McJ0048=;
        b=YJvycy7QIqtFW4d4aTfYPYstnzVUdZa4zGJlMgvXdsvCNY4FMKxXHorLS3yhOGCyfe
         RXNgz4Z9/dQRS735j/p6xdQzNjjEmrDvKJzdI9SclPC1qN4vUd0zzmk6Ajge8NrLWr3O
         G9Ga2dwQhP+fnwwOBmY0zuNqnpeDyM/wFSvy/Rfv513/7Cjo/WWktHvZSiuuwuGWmj/Q
         NVIb4fYDI9fOF6tW7PS3IBTO2dMIdaYWuzIpKfn4hT868n2q5vbBxK/Zlnw8a28bxxqp
         pPh3T/AnEPRr8aHZD4oLgDlTsQ+YdWSd0u5oi/68v4BBqH05HJtNqsusEXlrxQfS7j4J
         WbYA==
X-Gm-Message-State: ALoCoQlgpFqeBZePZUDV2taCwb+Oe8KOkB3RXGMnIJMzkZ2BR8keEvIjTQwX2UyN1HLDB7bu/SL4
X-Received: by 10.42.153.202 with SMTP id n10mr40446905icw.21.1433201673519;
        Mon, 01 Jun 2015 16:34:33 -0700 (PDT)
X-BeenThere: std-proposals@isocpp.org
Original-Received: by 10.140.41.169 with SMTP id z38ls2811756qgz.77.gmail; Mon, 01 Jun
 2015 16:34:32 -0700 (PDT)
X-Received: by 10.140.48.11 with SMTP id n11mr287051qga.35.1433201672815;
        Mon, 01 Jun 2015 16:34:32 -0700 (PDT)
In-Reply-To: <0af21d5d-e95e-41c9-8963-69be0cfbf366@isocpp.org>
X-Original-Sender: fmatthew5876@gmail.com
Precedence: list
Mailing-list: list std-proposals@isocpp.org; contact std-proposals+owners@isocpp.org
List-ID: <std-proposals.isocpp.org>
X-Google-Group-Id: 399137483710
List-Post: <http://groups.google.com/a/isocpp.org/group/std-proposals/post>, <mailto:std-proposals@isocpp.org>
List-Help: <http://support.google.com/a/isocpp.org/bin/topic.py?topic=25838>, <mailto:std-proposals+help@isocpp.org>
List-Archive: <http://groups.google.com/a/isocpp.org/group/std-proposals/>
List-Subscribe: <http://groups.google.com/a/isocpp.org/group/std-proposals/subscribe>,
 <mailto:std-proposals+subscribe@isocpp.org>
List-Unsubscribe: <mailto:googlegroups-manage+399137483710+unsubscribe@googlegroups.com>,
 <http://groups.google.com/a/isocpp.org/group/std-proposals/subscribe>
Xref: news.gmane.org gmane.comp.lang.c++.isocpp.proposals:18346
Archived-At: <http://permalink.gmane.org/gmane.comp.lang.c++.isocpp.proposals/18346>

------=_Part_4217_177766264.1433201672247
Content-Type: multipart/alternative; 
	boundary="----=_Part_4218_567846071.1433201672247"

------=_Part_4218_567846071.1433201672247
Content-Type: text/plain; charset=UTF-8

Just to make things more clear, here is a possible constructor format for 
in-place construction of move-only types. This kind of constructor could be 
used for any type, not just a hypothetical fixed_array.

//Class interface
template <typename T, typename A = std::allocator<T>>
class fixed_array {
  public:
    template <typename F, typename... Iterators>
    fixed_array(size_t n, F&& f, Iterators... iters);

    //Normal stuff like ::data(), operator[](int), etc...
  private:
    T* _begin;
    T* _end;
};

//Inplace constructor implementation
template <typename T,typename A> template <typename F, typename... Iterators
>
fixed_array<T,A>::fixed_array(size_t n, F&& f, Iterators... iters) {
   _begin = this->allocate(n);
   _end = _begin + n;

   struct Construct {
     Construct(A* alloc, T* p) : _alloc(alloc), _ptr(p) {}
     template <typename... Args> 
     T& operator()(Args&&... args) {
       _alloc->construct(_ptr, std::forward<Args>(args));
       return *_ptr;
     }
    private:
     A* _alloc;
     T* _ptr;
   };

   //exception safety left out for clarify
   for(size_t i = 0; i < n; ++i) {
      f(Construct(this, _begin + i), std::forward<Iterators>(iterators)...);
      advance_all_iterators(iterators...);
   }
}

//Usage examples
struct Foo { 
  Foo();
  Foo(int, double); 
  Foo(string);
  Foo(const Foo&) = delete; 
  Foo& operator=(const Foo&) = delete; 
};

auto ci = 0;
auto di = 0;
std::vector<int> iv = { 1, 2, 3 };
std::vector<double> dv = { 1.0, 2.0, 3.0 };

//Default construct 5 foos
auto fa0 = fixed_array<Foo>(5);

//Construct 5 foos using constants
auto fa1 = fixed_array<Foo>(5, [&](auto make) { make(ci, di); });

//Construct 3 foos using a constant and the elements of another container
auto fa2 = fixed_array<Foo>(3, [&](auto make, auto iiter) { make(*iiter, 
di); }, iv);

//Construct 3 foos using the elements of 2 different containers
auto fa3 = fixed_array<Foo>(3, [](auto make, auto iiter, auto diter) { 
make(*iiter, *diter); }, iv, dv);

//Use different constructors
auto fa4 = fixed_array<Foo>(3, [](auto make, auto iiter, auto diter) { 
if(*iiter < 2) { make(*iiter, *diter); } else { make("Hello"); }, iv, dv);


It seems like something like this wouldn't work for aggregates like 
std::array. I guess the only solution there would be to allow real 
copy/move elision for aggregate initialization.

-- 

--- 
You received this message because you are subscribed to the Google Groups "ISO C++ Standard - Future Proposals" group.
To unsubscribe from this group and stop receiving emails from it, send an email to std-proposals+unsubscribe@isocpp.org.
To post to this group, send email to std-proposals@isocpp.org.
Visit this group at http://groups.google.com/a/isocpp.org/group/std-proposals/.

------=_Part_4218_567846071.1433201672247
Content-Type: text/html; charset=UTF-8
Content-Transfer-Encoding: quoted-printable

<div dir=3D"ltr">Just to make things more clear, here is a possible constru=
ctor format for in-place construction of move-only types. This kind of cons=
tructor could be used for any type, not just a hypothetical fixed_array.<br=
><br><div class=3D"prettyprint" style=3D"background-color: rgb(250, 250, 25=
0); border-color: rgb(187, 187, 187); border-style: solid; border-width: 1p=
x; word-wrap: break-word;"><code class=3D"prettyprint"><div class=3D"subpre=
ttyprint"><span style=3D"color: #008;" class=3D"styled-by-prettify">//Class=
 interface<br>template</span><span style=3D"color: #000;" class=3D"styled-b=
y-prettify"> </span><span style=3D"color: #660;" class=3D"styled-by-prettif=
y">&lt;</span><span style=3D"color: #008;" class=3D"styled-by-prettify">typ=
ename</span><span style=3D"color: #000;" class=3D"styled-by-prettify"> T</s=
pan><span style=3D"color: #660;" class=3D"styled-by-prettify">, typename A =
=3D std::allocator&lt;T&gt;&gt;</span><span style=3D"color: #000;" class=3D=
"styled-by-prettify"><br></span><span style=3D"color: #008;" class=3D"style=
d-by-prettify">class</span><span style=3D"color: #000;" class=3D"styled-by-=
prettify"> fixed_array </span><span style=3D"color: #660;" class=3D"styled-=
by-prettify">{</span><span style=3D"color: #000;" class=3D"styled-by-pretti=
fy"><br>&nbsp; </span><span style=3D"color: #008;" class=3D"styled-by-prett=
ify">public</span><span style=3D"color: #660;" class=3D"styled-by-prettify"=
>:</span><span style=3D"color: #000;" class=3D"styled-by-prettify"><br>&nbs=
p; &nbsp; </span><span style=3D"color: #008;" class=3D"styled-by-prettify">=
template</span><span style=3D"color: #000;" class=3D"styled-by-prettify"> <=
/span><span style=3D"color: #660;" class=3D"styled-by-prettify">&lt;</span>=
<span style=3D"color: #008;" class=3D"styled-by-prettify">typename</span><s=
pan style=3D"color: #000;" class=3D"styled-by-prettify"> F</span><span styl=
e=3D"color: #660;" class=3D"styled-by-prettify">,</span><span style=3D"colo=
r: #000;" class=3D"styled-by-prettify"> </span><span style=3D"color: #008;"=
 class=3D"styled-by-prettify">typename</span><span style=3D"color: #660;" c=
lass=3D"styled-by-prettify">...</span><span style=3D"color: #000;" class=3D=
"styled-by-prettify"> </span><span style=3D"color: #606;" class=3D"styled-b=
y-prettify">Iterators</span><span style=3D"color: #660;" class=3D"styled-by=
-prettify">&gt;</span><span style=3D"color: #000;" class=3D"styled-by-prett=
ify"><br>&nbsp; &nbsp; fixed_array</span><span style=3D"color: #660;" class=
=3D"styled-by-prettify">(</span><span style=3D"color: #000;" class=3D"style=
d-by-prettify">size_t n</span><span style=3D"color: #660;" class=3D"styled-=
by-prettify">,</span><span style=3D"color: #000;" class=3D"styled-by-pretti=
fy"> F</span><span style=3D"color: #660;" class=3D"styled-by-prettify">&amp=
;&amp;</span><span style=3D"color: #000;" class=3D"styled-by-prettify"> f</=
span><span style=3D"color: #660;" class=3D"styled-by-prettify">,</span><spa=
n style=3D"color: #000;" class=3D"styled-by-prettify"> </span><span style=
=3D"color: #606;" class=3D"styled-by-prettify">Iterators</span><span style=
=3D"color: #660;" class=3D"styled-by-prettify">...</span><span style=3D"col=
or: #000;" class=3D"styled-by-prettify"> iters</span><span style=3D"color: =
#660;" class=3D"styled-by-prettify">);</span><span style=3D"color: #000;" c=
lass=3D"styled-by-prettify"><br><br>&nbsp; &nbsp; </span><span style=3D"col=
or: #800;" class=3D"styled-by-prettify">//Normal stuff like ::data(), opera=
tor[](int), etc...</span><span style=3D"color: #000;" class=3D"styled-by-pr=
ettify"><br>&nbsp; </span><span style=3D"color: #008;" class=3D"styled-by-p=
rettify">private</span><span style=3D"color: #660;" class=3D"styled-by-pret=
tify">:</span><span style=3D"color: #000;" class=3D"styled-by-prettify"><br=
>&nbsp; &nbsp; T</span><span style=3D"color: #660;" class=3D"styled-by-pret=
tify">*</span><span style=3D"color: #000;" class=3D"styled-by-prettify"> _b=
egin</span><span style=3D"color: #660;" class=3D"styled-by-prettify">;</spa=
n><span style=3D"color: #000;" class=3D"styled-by-prettify"><br>&nbsp; &nbs=
p; T</span><span style=3D"color: #660;" class=3D"styled-by-prettify">*</spa=
n><span style=3D"color: #000;" class=3D"styled-by-prettify"> _end</span><sp=
an style=3D"color: #660;" class=3D"styled-by-prettify">;</span><span style=
=3D"color: #000;" class=3D"styled-by-prettify"><br></span><span style=3D"co=
lor: #660;" class=3D"styled-by-prettify">};</span><span style=3D"color: #00=
0;" class=3D"styled-by-prettify"><br><br>//Inplace constructor implementati=
on<br></span><span style=3D"color: #008;" class=3D"styled-by-prettify">temp=
late</span><span style=3D"color: #000;" class=3D"styled-by-prettify"> </spa=
n><span style=3D"color: #660;" class=3D"styled-by-prettify">&lt;</span><spa=
n style=3D"color: #008;" class=3D"styled-by-prettify">typename</span><span =
style=3D"color: #000;" class=3D"styled-by-prettify"> T,typename A</span><sp=
an style=3D"color: #660;" class=3D"styled-by-prettify">&gt;</span><span sty=
le=3D"color: #000;" class=3D"styled-by-prettify"> </span><span style=3D"col=
or: #000;" class=3D"styled-by-prettify"><code class=3D"prettyprint"><span s=
tyle=3D"color: #008;" class=3D"styled-by-prettify">template</span><span sty=
le=3D"color: #000;" class=3D"styled-by-prettify"> </span><span style=3D"col=
or: #660;" class=3D"styled-by-prettify">&lt;</span><span style=3D"color: #0=
08;" class=3D"styled-by-prettify">typename</span><span style=3D"color: #000=
;" class=3D"styled-by-prettify"> F</span><span style=3D"color: #660;" class=
=3D"styled-by-prettify">,</span><span style=3D"color: #000;" class=3D"style=
d-by-prettify"> </span><span style=3D"color: #008;" class=3D"styled-by-pret=
tify">typename</span><span style=3D"color: #660;" class=3D"styled-by-pretti=
fy">...</span><span style=3D"color: #000;" class=3D"styled-by-prettify"> </=
span><span style=3D"color: #606;" class=3D"styled-by-prettify">Iterators</s=
pan><span style=3D"color: #660;" class=3D"styled-by-prettify">&gt;</span><s=
pan style=3D"color: #000;" class=3D"styled-by-prettify"><br></span></code>f=
ixed_array</span><span style=3D"color: #660;" class=3D"styled-by-prettify">=
&lt;</span><span style=3D"color: #000;" class=3D"styled-by-prettify">T</spa=
n><span style=3D"color: #660;" class=3D"styled-by-prettify">,A&gt;::</span>=
<span style=3D"color: #000;" class=3D"styled-by-prettify">fixed_array(size_=
t n, F&amp;&amp; f, Iterators... iters) {<br>&nbsp;&nbsp; _begin =3D this-&=
gt;allocate(n);<br>&nbsp;&nbsp; _end =3D _begin + n;<br><br>&nbsp;&nbsp; st=
ruct Construct {<br>&nbsp;&nbsp;&nbsp;&nbsp; Construct(A* alloc, T* p) : _a=
lloc(alloc), _ptr(p) {}<br>&nbsp;&nbsp; &nbsp; template &lt;typename... Arg=
s&gt; <br>&nbsp;&nbsp; &nbsp; T&amp; operator()(Args&amp;&amp;... args) {<b=
r>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; _alloc-&gt;construct(_ptr, std::forw=
ard&lt;Args&gt;(args));<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; return *_pt=
r;<br>&nbsp;&nbsp; &nbsp; }<br>&nbsp;&nbsp;&nbsp; private:<br>&nbsp;&nbsp;&=
nbsp;&nbsp; A* _alloc;<br>&nbsp;&nbsp;&nbsp;&nbsp; T* _ptr;<br>&nbsp;&nbsp;=
 };<br><br>&nbsp;&nbsp; //exception safety left out for clarify<br>&nbsp;&n=
bsp; for(size_t i =3D 0; i &lt; n; ++i) {<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;=
 f(Construct(this, _begin + i), std::forward&lt;Iterators&gt;(iterators)...=
);<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; advance_all_iterators(iterators...);<b=
r>&nbsp;&nbsp; }<br>}<br><br>//Usage examples<br>struct Foo { <br>&nbsp; Fo=
o();<br>&nbsp; Foo(int, double); <br>&nbsp; Foo(string);<br>&nbsp; Foo(cons=
t Foo&amp;) =3D delete; <br>&nbsp; Foo&amp; operator=3D(const Foo&amp;) =3D=
 delete; <br>};<br><br>auto ci =3D 0;<br>auto di =3D 0;<br>std::vector&lt;i=
nt&gt; iv =3D { 1, 2, 3 };<br>std::vector&lt;double&gt; dv =3D { 1.0, 2.0, =
3.0 };<br><br>//Default construct 5 foos<br>auto fa0 =3D fixed_array&lt;Foo=
&gt;(5);<br><br>//Construct 5 foos using constants<br>auto fa1 =3D fixed_ar=
ray&lt;Foo&gt;(5, [&amp;](auto make) { make(ci, di); });<br><br>//Construct=
 3 foos using a constant and the elements of another container<br>auto fa2 =
=3D fixed_array&lt;Foo&gt;(3, [&amp;](auto make, auto iiter) { make(*iiter,=
 di); }, iv);<br><br>//Construct 3 foos using the elements of 2 different c=
ontainers<br>auto fa3 =3D fixed_array&lt;Foo&gt;(3, [](auto make, auto iite=
r, auto diter) { make(*iiter, *diter); }, iv, dv);<br><br>//Use different c=
onstructors<br>auto fa4 =3D fixed_array&lt;Foo&gt;(3, [](auto make, auto ii=
ter, auto diter) { if(*iiter &lt; 2) { make(*iiter, *diter); } else { make(=
"Hello"); }, iv, dv);<br><br></span></div></code></div><br>It seems like so=
mething like this wouldn't work for aggregates like std::array. I guess the=
 only solution there would be to allow real copy/move elision for aggregate=
 initialization.<br></div>

<p></p>

-- <br />
<br />
--- <br />
You received this message because you are subscribed to the Google Groups &=
quot;ISO C++ Standard - Future Proposals&quot; group.<br />
To unsubscribe from this group and stop receiving emails from it, send an e=
mail to <a href=3D"mailto:std-proposals+unsubscribe@isocpp.org">std-proposa=
ls+unsubscribe@isocpp.org</a>.<br />
To post to this group, send email to <a href=3D"mailto:std-proposals@isocpp=
..org">std-proposals@isocpp.org</a>.<br />
Visit this group at <a href=3D"http://groups.google.com/a/isocpp.org/group/=
std-proposals/">http://groups.google.com/a/isocpp.org/group/std-proposals/<=
/a>.<br />

------=_Part_4218_567846071.1433201672247--
------=_Part_4217_177766264.1433201672247--

.
