220 32548 <5b87a9c6-b15d-465d-9d42-1620842e4f86@isocpp.org> article
Path: news.gmane.org!.POSTED!not-for-mail
From: ma.kalbfuss@web.de
Newsgroups: gmane.comp.lang.c++.isocpp.proposals
Subject: Re: relaxing rules for ternary operator. Allow
 incompatible types.
Date: Sun, 21 May 2017 07:07:54 -0700 (PDT)
Lines: 220
Approved: news@gmane.org
Message-ID: <5b87a9c6-b15d-465d-9d42-1620842e4f86@isocpp.org>
References: <1b5ee8eb-53df-4e98-af2f-829c7bc2e5b2@isocpp.org>
 <9064929.QEV8q21eIZ@tjmaciei-mobl1>
 <99350a9e-0c6b-468d-9761-f2b2b052275e@isocpp.org>
 <795341bd-d042-49c9-9eba-6b8ac6ca8a5e@isocpp.org>
Reply-To: std-proposals@isocpp.org
NNTP-Posting-Host: blaine.gmane.org
Mime-Version: 1.0
Content-Type: multipart/mixed; 
	boundary="----=_Part_2118_1299156135.1495375674459"
X-Trace: blaine.gmane.org 1495375675 6460 195.159.176.226 (21 May 2017 14:07:55 GMT)
X-Complaints-To: usenet@blaine.gmane.org
NNTP-Posting-Date: Sun, 21 May 2017 14:07:55 +0000 (UTC)
Cc: ma.kalbfuss@web.de
To: ISO C++ Standard - Future Proposals <std-proposals@isocpp.org>
Original-X-From: std-proposals+bncBC7YTI72XQOBBO56Q3EQKGQEGDM3M4I@isocpp.org Sun May 21 16:07:51 2017
Return-path: <std-proposals+bncBC7YTI72XQOBBO56Q3EQKGQEGDM3M4I@isocpp.org>
Envelope-to: gclcip-std-proposals@m.gmane.org
Original-Received: from mail-pg0-f69.google.com ([74.125.83.69])
	by blaine.gmane.org with esmtp (Exim 4.84_2)
	(envelope-from <std-proposals+bncBC7YTI72XQOBBO56Q3EQKGQEGDM3M4I@isocpp.org>)
	id 1dCRWQ-0001a1-Mh
	for gclcip-std-proposals@m.gmane.org; Sun, 21 May 2017 16:07:51 +0200
Original-Received: by mail-pg0-f69.google.com with SMTP id l125sf74783422pga.4
        for <gclcip-std-proposals@m.gmane.org>; Sun, 21 May 2017 07:07:56 -0700 (PDT)
DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed;
        d=isocpp-org.20150623.gappssmtp.com; s=20150623;
        h=date:from:to:cc:message-id:in-reply-to:references:subject
         :mime-version:x-original-sender:reply-to:precedence:mailing-list
         :list-id:x-spam-checked-in-group:list-post:list-help:list-archive
         :list-subscribe:list-unsubscribe;
        bh=tuhBg83H2RvGEWeIQHRxNSqRhxHY5i6NisxxGKAVdVw=;
        b=v5hFx+EL9VVNfWOLDA0LZ5AIbLbxv2iFzicDnii134q53uvd9GCUqnO1FpoWysLvNT
         rhPPfnPpNTNvR0AyJVhM4TIFkd8yJ1Oz/kq6uA/tho00N5ToqXDW7+enTFLCb6Hxldk+
         tFMn/gCEKFA8YkjKJmdPfsne8QI9qHhORdOFeUFeVrqpHJJ6m1w8XDchF7IZ2J4pMwYd
         uPqZc4c7qjFVWUM6LLNKnoouTYipmsNUvHEvLuv+g1dlcF6/BSpK0n4HPO0Dyt5JpoR8
         f7Y2oQrENAj/7JDH2987elo7tNbDq7rvJezRa05CoYFl1P54flgmnT3Pgmb9tUdQa5Fw
         hX2w==
X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed;
        d=1e100.net; s=20161025;
        h=x-gm-message-state:date:from:to:cc:message-id:in-reply-to
         :references:subject:mime-version:x-original-sender:reply-to
         :precedence:mailing-list:list-id:x-spam-checked-in-group:list-post
         :list-help:list-archive:list-subscribe:list-unsubscribe;
        bh=tuhBg83H2RvGEWeIQHRxNSqRhxHY5i6NisxxGKAVdVw=;
        b=gNormlGCCwRcFN5dkD0dDWSWMmm/ZbAqPWGGsTULgimHyUd4EN1BGsOscAHhyJVhP3
         L+qOUT0BckF8HPE/PmzJdYc3gNUB4u3E1NlxJ+c+uo04UzTV8sKm4fkAIV9vSyQo3ZKQ
         Rk0V5eq3ojfoyMj/INNfDDbbodhaBUgCSq4s1yswKaovDl4+ro2gFf/GvpcZuJ1D6ww2
         rCO/3q1zkUP3LCk/qhTw30f6xgXzVTXNlGLyIwguFE9i8CdmEchUJTFWH6sxynkgWmNM
         okm2gnqkx19X3yBOD/VzIbT/YbVbzOfUGyRknoYN/j2vCRU2iPBFXETIJuF4Wv4Dp8kF
         aqog==
X-Gm-Message-State: AODbwcD4uCBW6N6dtWjw+GHrPKGcrEMj/IB7AoxPPxw82k8Qf/+UMuy5
	Rb4MPvqYSLgjF9MK
X-Received: by 10.99.117.84 with SMTP id f20mr8567729pgn.149.1495375675838;
        Sun, 21 May 2017 07:07:55 -0700 (PDT)
X-BeenThere: std-proposals@isocpp.org
Original-Received: by 10.157.32.134 with SMTP id x6ls2233119ota.47.gmail; Sun, 21 May
 2017 07:07:54 -0700 (PDT)
X-Received: by 10.157.40.242 with SMTP id s105mr390007ota.5.1495375674928;
        Sun, 21 May 2017 07:07:54 -0700 (PDT)
In-Reply-To: <795341bd-d042-49c9-9eba-6b8ac6ca8a5e@isocpp.org>
X-Original-Sender: ma.kalbfuss@web.de
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: <https://groups.google.com/a/isocpp.org/group/std-proposals/post>, <mailto:std-proposals@isocpp.org>
List-Help: <https://support.google.com/a/isocpp.org/bin/topic.py?topic=25838>, <mailto:std-proposals+help@isocpp.org>
List-Archive: <https://groups.google.com/a/isocpp.org/group/std-proposals/>
List-Subscribe: <https://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>,
 <https://groups.google.com/a/isocpp.org/group/std-proposals/subscribe>
Xref: news.gmane.org gmane.comp.lang.c++.isocpp.proposals:32548
Archived-At: <http://permalink.gmane.org/gmane.comp.lang.c++.isocpp.proposals/32548>

------=_Part_2118_1299156135.1495375674459
Content-Type: multipart/alternative; 
	boundary="----=_Part_2119_2006947193.1495375674459"

------=_Part_2119_2006947193.1495375674459
Content-Type: text/plain; charset="UTF-8"

Am Sonntag, 21. Mai 2017 15:00:03 UTC+2 schrieb Mingxin Wang:
>
> You may as well consider the following situations:
>
> *Loops*
>
> It is difficult for compilers to generate code for loops with the feature 
> you proposed, e.g.:
>
> void foo(int n) {
>   /* A: Do something before the loop */
>   for (int i = 0; i < n; ++i) {
>     /* B: Do something */
>     auto x = runtime_condition() ? A{} : B{};
>     /* C: Do some other things with x */
>   }
>   /* D: Do something after the loop */
> }
> *Code Snippet 1*
>
> As far as I am concerned, here is the equivalent of the code that the 
> compiler would automatically generate for the code above (with your method):
>
> template <class T>
> void bar(int i, int n, T x) {
>   /* C: Do some other things with x */
>   ++i;
>   if (i < n) {
>     /* B: Do something */
>     if (runtime_condition()) {
>       bar(i, n, A{});
>     } else {
>       bar(i, n, B{});
>     }
>   }
> }
>
> void foo(int n) {
>   /* A: Do something before the loop */
>   if (0 < n) {
>     /* B: Do something */
>     if (runtime_condition()) {
>       bar(1, n, A{});
>     } else {
>       bar(1, n, B{});
>     }
>   }
>   /* D: Do something after the loop */
> }
> *Code Snippet 2*
>
> It is too complicated! Moreover, as the recursive call of the "function 
> template bar" is not able to be optimized, it will possibly increase the 
> runtime overhead.
>
> *Size*
>
> Compilers may generate a lot of redundant code when the feature is used 
> extensively, e.g.:
>
> auto x0 = runtime_condition_0() ? A0{} : B0{};
> auto x1 = runtime_condition_1() ? A1{} : B1{};
> // ...
> auto xn = runtime_condition_n() ? An{} : Bn{};
> *Code Snippet 3*
>
> As you introduced, the compiler is supposed to generate code for every 
> path! For the code above, it requires O(2^n) size for code generation, 
> which is NOT acceptable in most cases.
>
> In fact, the challenges above can be solved gracefully with the "proxies" 
> and the "wrappers".
>
> As the condition is determined at runtime, the concrete type cannot be 
> deduced at compile time. The only reason for us to save the returned value 
> is that *they have similar expressions and semantics*. We can simply 
> extract the common expressions of the types which are used in the context 
> to a "proxy", and save the returned value with a "wrapper", e.g. Code 
> Snippet 1 can be reconstructed as is shown below (with better performance 
> and smaller size):
>
> /* Declares the common expressions required in the context */
> proxy P {
>   void f_0();
>   double f_1(int, int);
> };
>
> void foo(int n) {
>   /* A: Do something before the loop */
>   for (int i = 0; i < n; ++i) {
>     /* B: Do something */
>     Wrapper<P> x(runtime_condition() ? Wrapper<P>(A{}) : Wrapper<P>(B{}));
>     P p = x.get_proxy();
>     /* C: Do some other things with p */
>   }
>   /* D: Do something after the loop */
> }
> *Code Snippet 4*
>
> Mingxin Wang
>

-- 
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.
To view this discussion on the web visit https://groups.google.com/a/isocpp.org/d/msgid/std-proposals/5b87a9c6-b15d-465d-9d42-1620842e4f86%40isocpp.org.

------=_Part_2119_2006947193.1495375674459
Content-Type: text/html; charset="UTF-8"
Content-Transfer-Encoding: quoted-printable

<div dir=3D"ltr">Am Sonntag, 21. Mai 2017 15:00:03 UTC+2 schrieb Mingxin Wa=
ng:<blockquote class=3D"gmail_quote" style=3D"margin: 0;margin-left: 0.8ex;=
border-left: 1px #ccc solid;padding-left: 1ex;"><div dir=3D"ltr"><div><font=
 face=3D"georgia, serif">You may as well consider the following situations:=
<br></font></div><div><font face=3D"georgia, serif"><br></font></div><div><=
div style=3D"font-family:georgia,serif"><b><font size=3D"4">Loops</font></b=
></div><div style=3D"font-family:georgia,serif"><br></div><div style=3D"fon=
t-family:georgia,serif">It is difficult for compilers to generate code for =
loops with the feature you proposed, e.g.:</div><div style=3D"font-family:g=
eorgia,serif"><br></div><div style=3D"font-family:georgia,serif"><div style=
=3D"border:1px solid rgb(187,187,187);word-wrap:break-word;background-color=
:rgb(250,250,250)"><code><div><div>void foo(int n) {</div><div>=C2=A0 /* A:=
 Do something before the loop */</div><div>=C2=A0 for (int i =3D 0; i &lt; =
n; ++i) {</div><div>=C2=A0 =C2=A0 /* B: Do something */</div><div>=C2=A0 =
=C2=A0 auto x =3D runtime_condition() ? A{} : B{};</div><div>=C2=A0 =C2=A0 =
/* C: Do some other things with x */</div><div>=C2=A0 }</div><div>=C2=A0 /*=
 D: Do something after the loop */</div><div>}</div></div></code></div><div=
 style=3D"text-align:center"><b>Code Snippet 1</b></div></div><div style=3D=
"font-family:georgia,serif"><br></div><div style=3D"font-family:georgia,ser=
if">As far as I am concerned, here is the equivalent of the code that the c=
ompiler would automatically generate for the code above (with your method):=
<br></div><div style=3D"font-family:georgia,serif"><br></div><div><div styl=
e=3D"border:1px solid rgb(187,187,187);word-wrap:break-word;background-colo=
r:rgb(250,250,250)"><code><div><font color=3D"#660066"><div>template &lt;cl=
ass T&gt;</div><div>void bar(int i, int n, T x) {</div><div>=C2=A0 /* C: Do=
 some other things with x */</div><div>=C2=A0 ++i;</div><div>=C2=A0 if (i &=
lt; n) {</div><div>=C2=A0 =C2=A0 /* B: Do something */</div><div>=C2=A0 =C2=
=A0 if (runtime_condition()) {</div><div>=C2=A0 =C2=A0 =C2=A0 bar(i, n, A{}=
);</div><div>=C2=A0 =C2=A0 } else {</div><div>=C2=A0 =C2=A0 =C2=A0 bar(i, n=
, B{});</div><div>=C2=A0 =C2=A0 }</div><div>=C2=A0 }</div><div>}</div><div>=
<br></div><div>void foo(int n) {</div><div>=C2=A0 /* A: Do something before=
 the loop */</div><div>=C2=A0 if (0 &lt; n) {</div><div>=C2=A0 =C2=A0 /* B:=
 Do something */</div><div>=C2=A0 =C2=A0 if (runtime_condition()) {</div><d=
iv>=C2=A0 =C2=A0 =C2=A0 bar(1, n, A{});</div><div>=C2=A0 =C2=A0 } else {</d=
iv><div>=C2=A0 =C2=A0 =C2=A0 bar(1, n, B{});</div><div>=C2=A0 =C2=A0 }</div=
><div>=C2=A0 }</div><div>=C2=A0 /* D: Do something after the loop */</div><=
div>}</div></font></div></code></div><div style=3D"text-align:center"><font=
 face=3D"georgia, serif"><b>Code Snippet 2</b></font></div><div><font face=
=3D"georgia, serif"><br></font></div><div><font face=3D"georgia, serif">It =
is too complicated! Moreover, as the recursive call of the &quot;function t=
emplate bar&quot; is not able to be optimized, it will possibly increase th=
e runtime overhead.</font></div><div><font face=3D"georgia, serif"><br></fo=
nt></div><div><font size=3D"4" face=3D"georgia, serif"><b>Size</b></font></=
div><div><font face=3D"georgia, serif"><br></font></div><div><font face=3D"=
georgia, serif">Compilers may generate a lot of redundant code when the fea=
ture is used extensively, e.g.:</font></div></div><div><font face=3D"georgi=
a, serif"><br></font></div><div><font face=3D"georgia, serif"><div style=3D=
"border:1px solid rgb(187,187,187);word-wrap:break-word;background-color:rg=
b(250,250,250)"><code><div><div>auto x0 =3D runtime_condition_0() ? A0{} : =
B0{};</div><div>auto x1 =3D runtime_condition_1() ? A1{} : B1{};</div><div>=
// ...</div><div>auto xn =3D runtime_condition_n() ? An{} : Bn{};</div></di=
v></code></div><div style=3D"text-align:center"><b>Code Snippet 3</b></div>=
<div><br></div><div>As you introduced, the compiler is supposed to generate=
 code for every path! For the code above, it requires O(2^n) size for code =
generation, which is NOT acceptable in most cases.</div><div><br></div><div=
>In fact, the challenges above can be solved gracefully with the &quot;prox=
ies&quot; and the &quot;wrappers&quot;.</div><div><br></div><div>As the con=
dition is determined at runtime, the concrete type cannot be deduced at com=
pile time. The only reason for us to save the returned value is that <b>the=
y have similar expressions and semantics</b>. We can simply extract the com=
mon expressions of the types which are used in the context to a &quot;proxy=
&quot;, and save the returned value with a &quot;wrapper&quot;, e.g. Code S=
nippet 1 can be reconstructed as is shown below (with better performance an=
d smaller size):</div></font></div><div><font face=3D"georgia, serif"><br><=
/font></div></div><div><div style=3D"border:1px solid rgb(187,187,187);word=
-wrap:break-word;background-color:rgb(250,250,250)"><code><div><font color=
=3D"#660066"><div>/* Declares the common expressions required in the contex=
t */</div><div>proxy P {</div><div>=C2=A0 void f_0();</div><div>=C2=A0 doub=
le f_1(int, int);</div><div>};</div><div><br></div><div>void foo(int n) {</=
div><div>=C2=A0 /* A: Do something before the loop */</div><div>=C2=A0 for =
(int i =3D 0; i &lt; n; ++i) {</div><div>=C2=A0 =C2=A0 /* B: Do something *=
/</div><div>=C2=A0 =C2=A0 Wrapper&lt;P&gt; x(runtime_condition() ? Wrapper&=
lt;P&gt;(A{}) : Wrapper&lt;P&gt;(B{}));</div><div>=C2=A0 =C2=A0 P p =3D x.g=
et_proxy();</div><div>=C2=A0 =C2=A0 /* C: Do some other things with p */</d=
iv><div>=C2=A0 }</div><div>=C2=A0 /* D: Do something after the loop */</div=
><div>}</div></font></div></code></div><div style=3D"text-align:center"><fo=
nt face=3D"georgia, serif"><b>Code Snippet 4</b></font></div></div><div sty=
le=3D"text-align:left"><font face=3D"georgia, serif"><br></font></div><div =
style=3D"text-align:left"><font face=3D"georgia, serif">Mingxin Wang</font>=
</div></div></blockquote></div>

<p></p>

-- <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 />
To view this discussion on the web visit <a href=3D"https://groups.google.c=
om/a/isocpp.org/d/msgid/std-proposals/5b87a9c6-b15d-465d-9d42-1620842e4f86%=
40isocpp.org?utm_medium=3Demail&utm_source=3Dfooter">https://groups.google.=
com/a/isocpp.org/d/msgid/std-proposals/5b87a9c6-b15d-465d-9d42-1620842e4f86=
%40isocpp.org</a>.<br />

------=_Part_2119_2006947193.1495375674459--

------=_Part_2118_1299156135.1495375674459--

.
