Topic: std::conditional pick() function?
Author: Tony V E <tvaneerd@gmail.com>
Date: Fri, 4 Apr 2014 13:37:27 -0400
Raw View
--001a11c36ca8b01d8e04f63af809
Content-Type: text/plain; charset=ISO-8859-1
Does anyone else (besides me) need a function like
template<bool B, typename X, typename Y>
std::conditional<B,X,Y>::type pick(X x, Y y);
that returns x or y (and type X or Y) based on the compile-time bool?
And if so, should it be part of std::conditional? ie
current std::conditional (ie boost::if_c)
template <bool cond, typename First, typename Second>
struct conditional // false case - second
{
typedef Second type;
};
template<typename First, typename Second>
struct conditional<true, First, Second> // true - pick first
{
typedef First type;
};
Proposal:
template <bool cond, typename First, typename Second>
struct conditional // false case - second
{
typedef Second type;
static Second pick(First , Second second)
{
return second;
}
};
template<typename First, typename Second>
struct conditional<true, First, Second> // true - pick first
{
typedef First type;
static First pick(First first, Second )
{
return first;
}
};
--
---
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/.
--001a11c36ca8b01d8e04f63af809
Content-Type: text/html; charset=ISO-8859-1
Content-Transfer-Encoding: quoted-printable
<div dir=3D"ltr"><div><div><div><div><div>Does anyone else (besides me) nee=
d a function like<br><br></div><div>=A0=A0=A0 template<bool B, typename =
X, typename Y><br>=A0=A0=A0 std::conditional<B,X,Y>::type pick(X x=
, Y y);<br>
<br></div><div>that returns x or y (and type X or Y) based on the compile-t=
ime bool?<br></div><br></div><div>And if so, should it be part of std::cond=
itional?=A0 ie<br><br></div><div><div>current std::conditional (ie boost::i=
f_c)<br>
<br>=A0=A0=A0 template <bool cond, typename First, typename Second><b=
r>=A0=A0=A0 struct conditional=A0 // false case - second<br>=A0=A0=A0 {<br>=
=A0=A0=A0=A0=A0=A0=A0 typedef Second type;<br>
=A0=A0=A0 };<br>=A0=A0=A0 template<typename First, typename Second><b=
r>=A0=A0=A0 struct conditional<true, First, Second> // true - pick fi=
rst<br>=A0=A0=A0 {<br>=A0=A0=A0=A0=A0=A0=A0 typedef First type;<br>=A0=A0=
=A0 };<br><br></div>Proposal:<br><br>=A0=A0=A0 template <bool cond, type=
name First, typename Second><br>
=A0=A0=A0 struct conditional=A0 // false case - second<br>=A0=A0=A0 {<br>=
=A0=A0=A0=A0=A0=A0=A0 typedef Second type;<br>=A0=A0=A0=A0=A0=A0=A0 static =
Second pick(First , Second second)<br>=A0=A0=A0=A0=A0=A0=A0 {<br>=A0=A0=A0=
=A0=A0=A0=A0=A0=A0=A0=A0 return second;<br>=A0=A0=A0=A0=A0=A0=A0 }<br>=A0=
=A0=A0 };<br>=A0=A0=A0 template<typename First, typename Second><br>
=A0=A0=A0 struct conditional<true, First, Second> // true - pick firs=
t<br>=A0=A0=A0 {<br>=A0=A0=A0=A0=A0=A0=A0 typedef First type;<br>=A0=A0=A0=
=A0=A0=A0=A0 static First pick(First first, Second )<br>=A0=A0=A0=A0=A0=A0=
=A0 {<br>=A0=A0=A0=A0=A0=A0=A0=A0=A0=A0=A0 return first;<br>=A0=A0=A0=A0=A0=
=A0=A0 }<br>
=A0=A0=A0 };<br>
<br></div><br></div></div></div></div>
<p></p>
-- <br />
<br />
--- <br />
You received this message because you are subscribed to the Google Groups &=
quot;ISO C++ Standard - Future Proposals" 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 />
--001a11c36ca8b01d8e04f63af809--
.
Author: Ville Voutilainen <ville.voutilainen@gmail.com>
Date: Fri, 4 Apr 2014 20:50:22 +0300
Raw View
On 4 April 2014 20:37, Tony V E <tvaneerd@gmail.com> wrote:
> Does anyone else (besides me) need a function like
>
> template<bool B, typename X, typename Y>
> std::conditional<B,X,Y>::type pick(X x, Y y);
>
> that returns x or y (and type X or Y) based on the compile-time bool?
I have certainly written a run-time version of it umpteen times. At those times
I didn't have the possibility to write a compile-time version. Whether that
is sufficient reason to standardize it is another matter. Very mildly
in favor...
--
---
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/.
.
Author: Zhihao Yuan <zy@miator.net>
Date: Fri, 4 Apr 2014 13:50:55 -0400
Raw View
On Fri, Apr 4, 2014 at 1:37 PM, Tony V E <tvaneerd@gmail.com> wrote:
> Does anyone else (besides me) need a function like
>
> template<bool B, typename X, typename Y>
> std::conditional<B,X,Y>::type pick(X x, Y y);
>
> that returns x or y (and type X or Y) based on the compile-time bool?
Nope, but if I do, I prefer to use C11 type generic expression:
#define PICK(cond, tv, fv) \
_Generic(bool_constant<bool(cond)>{}, \
std::true_type: (tv), \
std::false_type: (fv))
That gives you more flexibility, for example, you don't need to worry
about questions like "how to avoid the extra copy".
--
Zhihao Yuan, ID lichray
The best way to predict the future is to invent it.
___________________________________________________
4BSD -- http://4bsd.biz/
--
---
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/.
.
Author: Ville Voutilainen <ville.voutilainen@gmail.com>
Date: Fri, 4 Apr 2014 20:52:35 +0300
Raw View
On 4 April 2014 20:50, Zhihao Yuan <zy@miator.net> wrote:
> On Fri, Apr 4, 2014 at 1:37 PM, Tony V E <tvaneerd@gmail.com> wrote:
>> Does anyone else (besides me) need a function like
>>
>> template<bool B, typename X, typename Y>
>> std::conditional<B,X,Y>::type pick(X x, Y y);
>>
>> that returns x or y (and type X or Y) based on the compile-time bool?
>
> Nope, but if I do, I prefer to use C11 type generic expression:
>
> #define PICK(cond, tv, fv) \
> _Generic(bool_constant<bool(cond)>{}, \
> std::true_type: (tv), \
> std::false_type: (fv))
>
> That gives you more flexibility, for example, you don't need to worry
> about questions like "how to avoid the extra copy".
The way to avoid the "extra copy" is to move lvalues into the function,
and rvalues already avoid copies. I don't consider that a problem.
--
---
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/.
.
Author: Zhihao Yuan <zy@miator.net>
Date: Fri, 4 Apr 2014 14:07:34 -0400
Raw View
On Fri, Apr 4, 2014 at 1:52 PM, Ville Voutilainen
<ville.voutilainen@gmail.com> wrote:
>
> The way to avoid the "extra copy" is to move lvalues into the function,
> and rvalues already avoid copies. I don't consider that a problem.
In the first case, a std::move from the call side is required, plus
"move" is not doing nothing. And there is more,
PICK(blah, "\r\n", '\n')
-- No decay involved.
PICK(blah, lvalue, rvalue())
-- The value category is preserved.
--
Zhihao Yuan, ID lichray
The best way to predict the future is to invent it.
___________________________________________________
4BSD -- http://4bsd.biz/
--
---
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/.
.
Author: Tony V E <tvaneerd@gmail.com>
Date: Fri, 4 Apr 2014 14:10:47 -0400
Raw View
--089e0141a9a8e2660c04f63b6fe9
Content-Type: text/plain; charset=ISO-8859-1
On Fri, Apr 4, 2014 at 1:50 PM, Ville Voutilainen <
ville.voutilainen@gmail.com> wrote:
> On 4 April 2014 20:37, Tony V E <tvaneerd@gmail.com> wrote:
> > Does anyone else (besides me) need a function like
> >
> > template<bool B, typename X, typename Y>
> > std::conditional<B,X,Y>::type pick(X x, Y y);
> >
> > that returns x or y (and type X or Y) based on the compile-time bool?
>
>
> I have certainly written a run-time version of it umpteen times.
The runtime version can't return the right type. I need the type and the
value, based on, ie is_integral<X>, etc.
I find it helps build Concept-like functionality.
> At those times
> I didn't have the possibility to write a compile-time version. Whether that
> is sufficient reason to standardize it is another matter. Very mildly
> in favor...
>
> --
>
> ---
> 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/.
>
--
---
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/.
--089e0141a9a8e2660c04f63b6fe9
Content-Type: text/html; charset=ISO-8859-1
Content-Transfer-Encoding: quoted-printable
<div dir=3D"ltr"><br><div class=3D"gmail_extra"><br><br><div class=3D"gmail=
_quote">On Fri, Apr 4, 2014 at 1:50 PM, Ville Voutilainen <span dir=3D"ltr"=
><<a href=3D"mailto:ville.voutilainen@gmail.com" target=3D"_blank">ville=
..voutilainen@gmail.com</a>></span> wrote:<br>
<blockquote class=3D"gmail_quote" style=3D"margin:0 0 0 .8ex;border-left:1p=
x #ccc solid;padding-left:1ex"><div class=3D"">On 4 April 2014 20:37, Tony =
V E <<a href=3D"mailto:tvaneerd@gmail.com">tvaneerd@gmail.com</a>> wr=
ote:<br>
> Does anyone else (besides me) need a function like<br>
><br>
> =A0 =A0 template<bool B, typename X, typename Y><br>
> =A0 =A0 std::conditional<B,X,Y>::type pick(X x, Y y);<br>
><br>
> that returns x or y (and type X or Y) based on the compile-time bool?<=
br>
<br>
<br>
</div>I have certainly written a run-time version of it umpteen times. </bl=
ockquote><div><br></div><div><br>The runtime version can't return the r=
ight type.=A0 I need the type and the value, based on, ie is_integral<X&=
gt;, etc.<br>
</div><div>I find it helps build Concept-like functionality.<br></div><div>=
=A0</div><blockquote class=3D"gmail_quote" style=3D"margin:0 0 0 .8ex;borde=
r-left:1px #ccc solid;padding-left:1ex">At those times<br>
I didn't have the possibility to write a compile-time version. Whether =
that<br>
is sufficient reason to standardize it is another matter. Very mildly<br>
in favor...<br>
<span class=3D"HOEnZb"><font color=3D"#888888"><br>
--<br>
<br>
---<br>
You received this message because you are subscribed to the Google Groups &=
quot;ISO C++ Standard - Future Proposals" group.<br>
To unsubscribe from this group and stop receiving emails from it, send an e=
mail to <a href=3D"mailto:std-proposals%2Bunsubscribe@isocpp.org">std-propo=
sals+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/" target=3D"_blank">http://groups.google.com/a/isocpp.org/gro=
up/std-proposals/</a>.<br>
</font></span></blockquote></div><br></div></div>
<p></p>
-- <br />
<br />
--- <br />
You received this message because you are subscribed to the Google Groups &=
quot;ISO C++ Standard - Future Proposals" 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 />
--089e0141a9a8e2660c04f63b6fe9--
.
Author: David Krauss <potswa@gmail.com>
Date: Sat, 5 Apr 2014 14:18:41 +0800
Raw View
--Apple-Mail=_DB776959-7D28-4363-9555-2E47E7A27221
Content-Transfer-Encoding: quoted-printable
Content-Type: text/plain; charset=ISO-8859-1
This sounds an awful lot like a C11 type-generic expression. A TGE uses the=
type of an unevaluated controlling expression to select among several sube=
xpressions. The type and value of the TGE are those of the selected subexpr=
ession; the other subexpressions are not evaluated.
TGEs do more than std::conditional can do, but less than the proposed stati=
c_if. I think they just might be an appropriate solution.
- D
--=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/.
--Apple-Mail=_DB776959-7D28-4363-9555-2E47E7A27221
Content-Transfer-Encoding: quoted-printable
Content-Type: text/html; charset=ISO-8859-1
<html><head><meta http-equiv=3D"Content-Type" content=3D"text/html charset=
=3Diso-8859-1"></head><body style=3D"word-wrap: break-word; -webkit-nbsp-mo=
de: space; -webkit-line-break: after-white-space;">This sounds an awful lot=
like a C11 type-generic expression. A TGE uses the type of an unevaluated =
controlling expression to select among several subexpressions. The type and=
value of the TGE are those of the selected subexpression; the other subexp=
ressions are not evaluated.<div><br></div><div>TGEs do more than <font face=
=3D"Courier">std::conditional</font> can do, but less than the proposed&nbs=
p;<font face=3D"Courier">static_if</font>. I think they just might be an ap=
propriate solution.<br><div><br></div><div><span class=3D"Apple-tab-span" s=
tyle=3D"white-space:pre"> </span>- D</div><div><br></div></div></body></htm=
l>
<p></p>
-- <br />
<br />
--- <br />
You received this message because you are subscribed to the Google Groups &=
quot;ISO C++ Standard - Future Proposals" 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 />
--Apple-Mail=_DB776959-7D28-4363-9555-2E47E7A27221--
.