Topic: Interest on container rebind


Author: "Vicente J. Botet Escriba" <vicente.botet@wanadoo.fr>
Date: Thu, 23 May 2013 00:27:06 +0200
Raw View
Hi,

I need to transform a vector<optional<T>> into a optional<vector<T>> in
order to implement the following function

   optional<vector<T>>
   if_all(vector<optional<T>> const& c);

But also to transform set<optional<T>> into optional<set<T>>.

I would like to do it only once with a function

   template <typename C>
   //requires IsOptional(ValueType(C))
   optional<???> if_all(C const& c);

If STL containers provided rebind as Allocators does it would be
possible to define the preceding function  as

   template <typename C>
   //requires IsOptional(ValueType(C))
   optional<C::rebind<typename C::value_type::value_type>::other>
   if_all(C const& c);

Is there another way to do this without modifying the standard?
Is there an interest on such a possible rebind feature?

Best,
Vicente

--

---
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/?hl=en.



.


Author: Richard Smith <richard@metafoo.co.uk>
Date: Wed, 22 May 2013 15:46:20 -0700
Raw View
--047d7bf0e8dea0301104dd5655ca
Content-Type: text/plain; charset=ISO-8859-1

On Wed, May 22, 2013 at 3:27 PM, Vicente J. Botet Escriba <
vicente.botet@wanadoo.fr> wrote:

> Hi,
>
> I need to transform a vector<optional<T>> into a optional<vector<T>> in
> order to implement the following function
>
>   optional<vector<T>>
>   if_all(vector<optional<T>> const& c);
>
> But also to transform set<optional<T>> into optional<set<T>>.
>
> I would like to do it only once with a function
>
>   template <typename C>
>   //requires IsOptional(ValueType(C))
>   optional<???> if_all(C const& c);
>
> If STL containers provided rebind as Allocators does it would be possible
> to define the preceding function  as
>
>   template <typename C>
>   //requires IsOptional(ValueType(C))
>   optional<C::rebind<typename C::value_type::value_type>::**other>
>   if_all(C const& c);
>
> Is there another way to do this without modifying the standard?
>

Sure. You can write a rebind template and specialize it for each kind of
container you want to rebind.

template<typename T, typename U> struct rebind_container_impl;
template<typename T, typename Alloc, typename U> struct
rebind_container_impl<std::vector<T, Alloc>, U> {
  using type = std::vector<U, typename
std::allocator_traits<Alloc>::template rebind_alloc<U>>;
};
// ...
template<typename T, typename U> using rebind_container = typename
rebind_container_impl<T, U>::type;

--

---
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/?hl=en.



--047d7bf0e8dea0301104dd5655ca
Content-Type: text/html; charset=ISO-8859-1
Content-Transfer-Encoding: quoted-printable

On Wed, May 22, 2013 at 3:27 PM, Vicente J. Botet Escriba <span dir=3D"ltr"=
>&lt;<a href=3D"mailto:vicente.botet@wanadoo.fr" target=3D"_blank">vicente.=
botet@wanadoo.fr</a>&gt;</span> wrote:<br><div class=3D"gmail_quote"><block=
quote class=3D"gmail_quote" style=3D"margin:0 0 0 .8ex;border-left:1px #ccc=
 solid;padding-left:1ex">
Hi,<br>
<br>
I need to transform a vector&lt;optional&lt;T&gt;&gt; into a optional&lt;ve=
ctor&lt;T&gt;&gt; in order to implement the following function<br>
<br>
=A0 optional&lt;vector&lt;T&gt;&gt;<br>
=A0 if_all(vector&lt;optional&lt;T&gt;&gt; const&amp; c);<br>
<br>
But also to transform set&lt;optional&lt;T&gt;&gt; into optional&lt;set&lt;=
T&gt;&gt;.<br>
<br>
I would like to do it only once with a function<br>
<br>
=A0 template &lt;typename C&gt;<br>
=A0 //requires IsOptional(ValueType(C))<br>
=A0 optional&lt;???&gt; if_all(C const&amp; c);<br>
<br>
If STL containers provided rebind as Allocators does it would be possible t=
o define the preceding function =A0as<br>
<br>
=A0 template &lt;typename C&gt;<br>
=A0 //requires IsOptional(ValueType(C))<br>
=A0 optional&lt;C::rebind&lt;typename C::value_type::value_type&gt;::<u></u=
>other&gt;<br>
=A0 if_all(C const&amp; c);<br>
<br>
Is there another way to do this without modifying the standard?<br></blockq=
uote><div><br></div><div>Sure. You can write a rebind template and speciali=
ze it for each kind of container you want to rebind.</div><div><br></div>
<div>template&lt;typename T, typename U&gt; struct rebind_container_impl;</=
div><div>template&lt;typename T, typename Alloc, typename U&gt; struct rebi=
nd_container_impl&lt;std::vector&lt;T, Alloc&gt;, U&gt; {</div><div>=A0 usi=
ng type =3D std::vector&lt;U, typename std::allocator_traits&lt;Alloc&gt;::=
template rebind_alloc&lt;U&gt;&gt;;</div>
<div>};</div><div>// ...</div><div>template&lt;typename T, typename U&gt; u=
sing rebind_container =3D typename rebind_container_impl&lt;T, U&gt;::type;=
</div></div>

<p></p>

-- <br />
&nbsp;<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 std-proposals+unsubscribe@isocpp.org.<br />
To post to this group, send email to std-proposals@isocpp.org.<br />
Visit this group at <a href=3D"http://groups.google.com/a/isocpp.org/group/=
std-proposals/?hl=3Den">http://groups.google.com/a/isocpp.org/group/std-pro=
posals/?hl=3Den</a>.<br />
&nbsp;<br />
&nbsp;<br />

--047d7bf0e8dea0301104dd5655ca--

.


Author: "Vicente J. Botet Escriba" <vicente.botet@wanadoo.fr>
Date: Thu, 23 May 2013 07:43:03 +0200
Raw View
This is a multi-part message in MIME format.
--------------040505070407040004070105
Content-Type: text/plain; charset=ISO-8859-1; format=flowed
Content-Transfer-Encoding: quoted-printable

Le 23/05/13 00:46, Richard Smith a =E9crit :
> On Wed, May 22, 2013 at 3:27 PM, Vicente J. Botet Escriba=20
> <vicente.botet@wanadoo.fr <mailto:vicente.botet@wanadoo.fr>> wrote:
>
>     Hi,
>
>     I need to transform a vector<optional<T>> into a
>     optional<vector<T>> in order to implement the following function
>
>       optional<vector<T>>
>       if_all(vector<optional<T>> const& c);
>
>     But also to transform set<optional<T>> into optional<set<T>>.
>
>     I would like to do it only once with a function
>
>       template <typename C>
>       //requires IsOptional(ValueType(C))
>       optional<???> if_all(C const& c);
>
>     If STL containers provided rebind as Allocators does it would be
>     possible to define the preceding function  as
>
>       template <typename C>
>       //requires IsOptional(ValueType(C))
>       optional<C::rebind<typename C::value_type::value_type>::other>
>       if_all(C const& c);
>
>     Is there another way to do this without modifying the standard?
>
>
> Sure. You can write a rebind template and specialize it for each kind=20
> of container you want to rebind.
>
> template<typename T, typename U> struct rebind_container_impl;
> template<typename T, typename Alloc, typename U> struct=20
> rebind_container_impl<std::vector<T, Alloc>, U> {
>   using type =3D std::vector<U, typename=20
> std::allocator_traits<Alloc>::template rebind_alloc<U>>;
> };
> // ...
> template<typename T, typename U> using rebind_container =3D typename=20
> rebind_container_impl<T, U>::type;
>
Yes, I know I can do this. But this wouldn't work for a library, as the=20
library doesn't know all the containers.

Best,
Vicente

--=20

---=20
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 e=
mail 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-proposa=
ls/?hl=3Den.



--------------040505070407040004070105
Content-Type: text/html; charset=ISO-8859-1

<html>
  <head>
    <meta content="text/html; charset=ISO-8859-1"
      http-equiv="Content-Type">
  </head>
  <body bgcolor="#FFFFFF" text="#000000">
    <div class="moz-cite-prefix">Le 23/05/13 00:46, Richard Smith a
      &eacute;crit&nbsp;:<br>
    </div>
    <blockquote
cite="mid:CAOfiQqnW3ZsVb1+U2q+S7Y6JWNJyd6TC+tqatSsenKfzFjMjOA@mail.gmail.com"
      type="cite">On Wed, May 22, 2013 at 3:27 PM, Vicente J. Botet
      Escriba <span dir="ltr">&lt;<a moz-do-not-send="true"
          href="mailto:vicente.botet@wanadoo.fr" target="_blank">vicente.botet@wanadoo.fr</a>&gt;</span>
      wrote:<br>
      <div class="gmail_quote">
        <blockquote class="gmail_quote" style="margin:0 0 0
          .8ex;border-left:1px #ccc solid;padding-left:1ex">
          Hi,<br>
          <br>
          I need to transform a vector&lt;optional&lt;T&gt;&gt; into a
          optional&lt;vector&lt;T&gt;&gt; in order to implement the
          following function<br>
          <br>
          &nbsp; optional&lt;vector&lt;T&gt;&gt;<br>
          &nbsp; if_all(vector&lt;optional&lt;T&gt;&gt; const&amp; c);<br>
          <br>
          But also to transform set&lt;optional&lt;T&gt;&gt; into
          optional&lt;set&lt;T&gt;&gt;.<br>
          <br>
          I would like to do it only once with a function<br>
          <br>
          &nbsp; template &lt;typename C&gt;<br>
          &nbsp; //requires IsOptional(ValueType(C))<br>
          &nbsp; optional&lt;???&gt; if_all(C const&amp; c);<br>
          <br>
          If STL containers provided rebind as Allocators does it would
          be possible to define the preceding function &nbsp;as<br>
          <br>
          &nbsp; template &lt;typename C&gt;<br>
          &nbsp; //requires IsOptional(ValueType(C))<br>
          &nbsp; optional&lt;C::rebind&lt;typename
          C::value_type::value_type&gt;::other&gt;<br>
          &nbsp; if_all(C const&amp; c);<br>
          <br>
          Is there another way to do this without modifying the
          standard?<br>
        </blockquote>
        <div><br>
        </div>
        <div>Sure. You can write a rebind template and specialize it for
          each kind of container you want to rebind.</div>
        <div><br>
        </div>
        <div>template&lt;typename T, typename U&gt; struct
          rebind_container_impl;</div>
        <div>template&lt;typename T, typename Alloc, typename U&gt;
          struct rebind_container_impl&lt;std::vector&lt;T, Alloc&gt;,
          U&gt; {</div>
        <div>&nbsp; using type = std::vector&lt;U, typename
          std::allocator_traits&lt;Alloc&gt;::template
          rebind_alloc&lt;U&gt;&gt;;</div>
        <div>};</div>
        <div>// ...</div>
        <div>template&lt;typename T, typename U&gt; using
          rebind_container = typename rebind_container_impl&lt;T,
          U&gt;::type;</div>
      </div>
      <br>
    </blockquote>
    Yes, I know I can do this. But this wouldn't work for a library, as
    the library doesn't know all the containers.<br>
    <br>
    Best,<br>
    Vicente<br>
  </body>
</html>

<p></p>

-- <br />
&nbsp;<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 email to std-proposals+unsubscribe@isocpp.org.<br />
To post to this group, send email to std-proposals@isocpp.org.<br />
Visit this group at <a href="http://groups.google.com/a/isocpp.org/group/std-proposals/?hl=en">http://groups.google.com/a/isocpp.org/group/std-proposals/?hl=en</a>.<br />
&nbsp;<br />
&nbsp;<br />

--------------040505070407040004070105--

.


Author: DeadMG <wolfeinstein@gmail.com>
Date: Thu, 23 May 2013 13:47:07 -0700 (PDT)
Raw View
------=_Part_619_3508018.1369342027024
Content-Type: text/plain; charset=ISO-8859-1

You must use a template template. Check out the automated rebind
implementation in allocator_traits.

--

---
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/?hl=en.



------=_Part_619_3508018.1369342027024
Content-Type: text/html; charset=ISO-8859-1

You must use a template template. Check out the automated rebind implementation in allocator_traits.

<p></p>

-- <br />
&nbsp;<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 email to std-proposals+unsubscribe@isocpp.org.<br />
To post to this group, send email to std-proposals@isocpp.org.<br />
Visit this group at <a href="http://groups.google.com/a/isocpp.org/group/std-proposals/?hl=en">http://groups.google.com/a/isocpp.org/group/std-proposals/?hl=en</a>.<br />
&nbsp;<br />
&nbsp;<br />

------=_Part_619_3508018.1369342027024--

.


Author: "Vicente J. Botet Escriba" <vicente.botet@wanadoo.fr>
Date: Fri, 24 May 2013 21:15:43 +0200
Raw View
Le 23/05/13 22:47, DeadMG a =E9crit :
> You must use a template template. Check out the automated rebind=20
> implementation in allocator_traits. --
>
This is exactly what I try to avoid, as I don't want to force the end=20
user to give me a template template instead of a template container=20
which can be deduced.

Vicente

--=20

---=20
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 e=
mail 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-proposa=
ls/?hl=3Den.



.


Author: DeadMG <wolfeinstein@gmail.com>
Date: Fri, 24 May 2013 12:49:53 -0700 (PDT)
Raw View
------=_Part_570_14663605.1369424993527
Content-Type: text/plain; charset=ISO-8859-1

The user doesn't have to give you a template template. You don't pass
allocators as template templates, but allocator_traits::rebind can still
automatically implement rebind in the vast majority of cases. Look at it.

--

---
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/?hl=en.



------=_Part_570_14663605.1369424993527
Content-Type: text/html; charset=ISO-8859-1

The user doesn't have to give you a template template. You don't pass allocators as template templates, but allocator_traits::rebind can still automatically implement rebind in the vast majority of cases. Look at it.

<p></p>

-- <br />
&nbsp;<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 email to std-proposals+unsubscribe@isocpp.org.<br />
To post to this group, send email to std-proposals@isocpp.org.<br />
Visit this group at <a href="http://groups.google.com/a/isocpp.org/group/std-proposals/?hl=en">http://groups.google.com/a/isocpp.org/group/std-proposals/?hl=en</a>.<br />
&nbsp;<br />
&nbsp;<br />

------=_Part_570_14663605.1369424993527--

.


Author: "Vicente J. Botet Escriba" <vicente.botet@wanadoo.fr>
Date: Sat, 25 May 2013 15:06:31 +0200
Raw View
Le 24/05/13 21:49, DeadMG a =E9crit :
> The user doesn't have to give you a template template. You don't pass=20
> allocators as template templates, but allocator_traits::rebind can=20
> still automatically implement rebind in the vast majority of cases.=20
> Look at it. --
>
I don't get what you try to tell me. Please, could you clarify how=20
allocator_traits::rebind could help on what I want to do. Could you show=20
me how to declare my original function

   template <typename C>
   //requires IsOptional(ValueType(C))
   optional<???> if_all(C const& c);

so that it works or all the models of container without modifying the=20
standard?

Best,
Vicente

--=20

---=20
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 e=
mail 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-proposa=
ls/?hl=3Den.



.


Author: Jeffrey Yasskin <jyasskin@google.com>
Date: Sat, 25 May 2013 09:17:10 -0700
Raw View
On Sat, May 25, 2013 at 6:06 AM, Vicente J. Botet Escriba
<vicente.botet@wanadoo.fr> wrote:
> Le 24/05/13 21:49, DeadMG a =E9crit :
>
>> The user doesn't have to give you a template template. You don't pass
>> allocators as template templates, but allocator_traits::rebind can still
>> automatically implement rebind in the vast majority of cases. Look at it=
.. --
>>
> I don't get what you try to tell me. Please, could you clarify how
> allocator_traits::rebind could help on what I want to do. Could you show =
me
> how to declare my original function
>
>
>   template <typename C>
>   //requires IsOptional(ValueType(C))
>   optional<???> if_all(C const& c);
>
> so that it works or all the models of container without modifying the
> standard?

I think he meant "look at your standard library's implementation of
allocator_traits::rebind in order to answer your question yourself."

--=20

---=20
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 e=
mail 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-proposa=
ls/?hl=3Den.



.


Author: "Vicente J. Botet Escriba" <vicente.botet@wanadoo.fr>
Date: Sat, 25 May 2013 19:42:45 +0200
Raw View
Le 25/05/13 18:17, Jeffrey Yasskin a =E9crit :
> On Sat, May 25, 2013 at 6:06 AM, Vicente J. Botet Escriba
> <vicente.botet@wanadoo.fr> wrote:
>> Le 24/05/13 21:49, DeadMG a =E9crit :
>>
>>> The user doesn't have to give you a template template. You don't pass
>>> allocators as template templates, but allocator_traits::rebind can stil=
l
>>> automatically implement rebind in the vast majority of cases. Look at i=
t. --
>>>
>> I don't get what you try to tell me. Please, could you clarify how
>> allocator_traits::rebind could help on what I want to do. Could you show=
 me
>> how to declare my original function
>>
>>
>>    template <typename C>
>>    //requires IsOptional(ValueType(C))
>>    optional<???> if_all(C const& c);
>>
>> so that it works or all the models of container without modifying the
>> standard?
> I think he meant "look at your standard library's implementation of
> allocator_traits::rebind in order to answer your question yourself."
>
Thanks to both for all your help. I was surely looking in the wrong=20
direction or reading too quickly the posts.
You have never had the solution in face of you and not be able to=20
getting it at all?

Anyway, IIUC the solution is partial. The default trait of this solution=20
(Alloc<T, Args>) works only if all the container parameters are types=20
and the first one is the type to rebind.

   template <class C, class T> using rebind =3D typename=20
detail::traits_rebind<C, T>::type;

For container having non-type templates, the definition must include its=20
own rebind, e.g.

   template <typename T, size_t N>
   struct my_staticaly_bounded_container // std::array?
{
     template <class U> using rebind =3D my_staticaly_bounded_container<U, =
N>;
   };

Is this correct?

If yes, is it wort adding it to the standard so that the users (as me)=20
can define

   template <typename C>
   //requires IsOptional(ValueType(C))
   optional<rebind<C, typename C::value_type::value_type>> if_all(C=20
const& c);

Vicente

--=20

---=20
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 e=
mail 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-proposa=
ls/?hl=3Den.



.


Author: DeadMG <wolfeinstein@gmail.com>
Date: Sat, 25 May 2013 10:46:07 -0700 (PDT)
Raw View
------=_Part_1389_7329698.1369503967236
Content-Type: text/plain; charset=ISO-8859-1

You could simply special-case std::array. However, ultimately, what you are
trying to do plain doesn't make sense for all containers- for example, you
can't rebind just one type on unordered_map, and even if you did, the
hasher and such would be wrong. Your allocator will be incorrect as well if
you only rebind the first type- you will have to find which parameter is
the allocator and rebind it too.

--

---
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/?hl=en.



------=_Part_1389_7329698.1369503967236
Content-Type: text/html; charset=ISO-8859-1
Content-Transfer-Encoding: quoted-printable

You could simply special-case std::array. However, ultimately, what you are=
 trying to do plain doesn't make sense for all containers- for example, you=
 can't rebind just one type on unordered_map, and even if you did, the hash=
er and such would be wrong. Your allocator will be incorrect as well if you=
 only rebind the first type- you will have to find which parameter is the a=
llocator and rebind it too.

<p></p>

-- <br />
&nbsp;<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 std-proposals+unsubscribe@isocpp.org.<br />
To post to this group, send email to std-proposals@isocpp.org.<br />
Visit this group at <a href=3D"http://groups.google.com/a/isocpp.org/group/=
std-proposals/?hl=3Den">http://groups.google.com/a/isocpp.org/group/std-pro=
posals/?hl=3Den</a>.<br />
&nbsp;<br />
&nbsp;<br />

------=_Part_1389_7329698.1369503967236--

.