220 22141 <73b5fa06-b0e2-4f64-96a5-703fbdde4aa2@isocpp.org> article
Path: news.gmane.org!not-for-mail
From: Nicol Bolas <jmckesson@gmail.com>
Newsgroups: gmane.comp.lang.c++.isocpp.proposals
Subject: Re: N4542: Should we be able to "widen" variant<A,B>
 to variant<A,B,C>, or "narrow" it to variant<A>?
Date: Thu, 29 Oct 2015 16:10:23 -0700 (PDT)
Lines: 198
Approved: news@gmane.org
Message-ID: <73b5fa06-b0e2-4f64-96a5-703fbdde4aa2@isocpp.org>
References: <e9ca2bb3-89ae-4b10-a865-f1c4eee7bdc3@isocpp.org>
Reply-To: std-proposals@isocpp.org
NNTP-Posting-Host: plane.gmane.org
Mime-Version: 1.0
Content-Type: multipart/mixed; 
	boundary="----=_Part_14_1132156555.1446160224012"
X-Trace: ger.gmane.org 1446160229 373 80.91.229.3 (29 Oct 2015 23:10:29 GMT)
X-Complaints-To: usenet@ger.gmane.org
NNTP-Posting-Date: Thu, 29 Oct 2015 23:10:29 +0000 (UTC)
To: ISO C++ Standard - Future Proposals <std-proposals@isocpp.org>
Original-X-From: std-proposals+bncBCEKFTV6ZUMBBYOOZKYQKGQEYYNXD5Q@isocpp.org Fri Oct 30 00:10:27 2015
Return-path: <std-proposals+bncBCEKFTV6ZUMBBYOOZKYQKGQEYYNXD5Q@isocpp.org>
Envelope-to: gclcip-std-proposals@m.gmane.org
Original-Received: from mail-ig0-f198.google.com ([209.85.213.198])
	by plane.gmane.org with esmtp (Exim 4.69)
	(envelope-from <std-proposals+bncBCEKFTV6ZUMBBYOOZKYQKGQEYYNXD5Q@isocpp.org>)
	id 1ZrwKx-0005Mc-4S
	for gclcip-std-proposals@m.gmane.org; Fri, 30 Oct 2015 00:10:27 +0100
Original-Received: by igbxf8 with SMTP id xf8sf119319255igb.1
        for <gclcip-std-proposals@m.gmane.org>; Thu, 29 Oct 2015 16:10:26 -0700 (PDT)
DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed;
        d=gmail.com; s=20120113;
        h=date:from:to:message-id:in-reply-to:references:subject:mime-version
         :content-type: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=7DQh4eOlq6Gzj7yfp5BJ3uCFl96vt7HSY2/HsWSAHEk=;
        b=QsK+H3qq3wBAMrnfZwZV/p2TG/mpMu2rJyKYx+iw2rGxIISi2j6QKxaxD8AIfSG94v
         TauQRqXpr6aslT9M8mfduJW9qD6XFGwGNFrBRH5vjGiT4QW41IT/CjL+nZUJ+HU7M7JG
         D/XNmEEB3gZbdfDWLNiFPYdxKRWSXl0YxnZ/U9BjtCXO33/CMFJKO1LvMf5qIAe1B17h
         tiJ+mHgBsQj7sDCTDdHsdne5MN+mtKFFw/JDL3M6sjE9hjic1epQo0X3efx+9F7jCcU/
         Pdhh9Jro5dsw0FaSpc6cA3SXI1DBvcOUM1Zl+Rj7N1AkZ9VEa4xN3gJqTZPNN7M6NCGv
         tE6g==
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:message-id:in-reply-to:references
         :subject:mime-version:content-type: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=7DQh4eOlq6Gzj7yfp5BJ3uCFl96vt7HSY2/HsWSAHEk=;
        b=jhpNsA2cWDe4m8Ovn3uhE8Vx8ygpdNYbq/G87nr78aKrPapDWy4QHHQ1puDhwE7PbE
         wiM2nw8qULzI44KnpOdP+LLHwo8Dsc+FrTz8y6NvJTNN5RZ9s3eaERQpGBLS0pQ6Yj5I
         YOAxr/PaMdzGhoDKn2imQSkIHik8S9FFasyGt9syhQjVek1VaojPO/wIPLYtSwegXFLw
         HmY8/hbZs3PF40GqjSLxBFuMsHBYBU9hJjzIkeCrQQnsPy23Eup7dQddFvAN2Rtfm0MU
         1syw2wD5hteoeYAUwtoVvEsGb554376SPPgqVziC2LcWOlaIHaYh53pVrMRv1TOoBqGZ
         +CyA==
X-Gm-Message-State: ALoCoQmnS25haytVbGQjv+7ukdWgYAXhOXZzf6nwOdlcMGV6xeQGp3pRegO4vTrhk+Q7HrzX45m+
X-Received: by 10.107.47.219 with SMTP id v88mr4776426iov.25.1446160226062;
        Thu, 29 Oct 2015 16:10:26 -0700 (PDT)
X-BeenThere: std-proposals@isocpp.org
Original-Received: by 10.107.27.83 with SMTP id b80ls449429iob.20.gmail; Thu, 29 Oct
 2015 16:10:25 -0700 (PDT)
X-Received: by 10.50.77.70 with SMTP id q6mr359577igw.4.1446160225067;
        Thu, 29 Oct 2015 16:10:25 -0700 (PDT)
In-Reply-To: <e9ca2bb3-89ae-4b10-a865-f1c4eee7bdc3@isocpp.org>
X-Original-Sender: jmckesson@gmail.com
Precedence: list
Mailing-list: list std-proposals@isocpp.org; contact std-proposals+owners@isocpp.org
List-ID: <std-proposals.isocpp.org>
X-Spam-Checked-In-Group: 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:22141
Archived-At: <http://permalink.gmane.org/gmane.comp.lang.c++.isocpp.proposals/22141>

------=_Part_14_1132156555.1446160224012
Content-Type: multipart/alternative; 
	boundary="----=_Part_15_2090865752.1446160224012"

------=_Part_15_2090865752.1446160224012
Content-Type: text/plain; charset=UTF-8

On Thursday, October 29, 2015 at 6:09:38 PM UTC-4, Arthur O'Dwyer wrote:
>
> This question came up at the Mountain View meetup last night.
>
> I can imagine wanting to do this:
>
>     using Value = std::variant<int,double,std::string,List,Object>;
>     using Simple = std::variant<int,double,std::string>;
>     using Numeric = std::variant<int,double>;
>
>     void func(Simple x);
>
>     Numeric n;
>     func(n);  // Should this work, somehow, since every Numeric "is-a" 
> Simple in some sense?
>

Could it work? Yes. Should it work? Not implicitly; if it should be 
allowed, there ought to be a function for it. When should it work? Well, 
that's the question.
 

>
>     Value v;
>     func(v);  // Should this compile, somehow, and then throw at runtime 
> if v holds a List or Object?
>

No, it should not. Not implicitly.

The "narrowing" case is sort of like dynamic_cast;
>

In no sense is it like a dynamic cast. `dynamic_cast` is about casting 
within a class hierarchy. Specifically, from child to parent. The only way 
it's similar is that it can conceptually fail. 

Or, is there a clever trick to get this kind of behavior already, without 
> having to write an arcane and expensive helper function that does a bunch 
> of get()s and assignments?
>

There is nothing arcane about a simple visitor:

template<typename ...Args>
variant_conv
{
  template<typename T>
  variant<Args...> operator()(const T &t) const
  {
    //If T not in Args, throw.
    return t;
  }
};

auto v = visit(variant_conv<int, float, std::string>{}, variant<int, float>{
3.4f});

You can even wrap that up in a template function.

-- 

--- 
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_15_2090865752.1446160224012
Content-Type: text/html; charset=UTF-8
Content-Transfer-Encoding: quoted-printable

<div dir=3D"ltr">On Thursday, October 29, 2015 at 6:09:38 PM UTC-4, Arthur =
O&#39;Dwyer wrote:<blockquote class=3D"gmail_quote" style=3D"margin: 0;marg=
in-left: 0.8ex;border-left: 1px #ccc solid;padding-left: 1ex;"><div dir=3D"=
ltr">This question came up at the Mountain View meetup last night.<div><br>=
</div><div>I can imagine wanting to do this:</div><div><br></div><div>=C2=
=A0 =C2=A0 using Value =3D std::variant&lt;int,double,std::<wbr>string,List=
,Object&gt;;</div><div>=C2=A0 =C2=A0 using Simple =3D std::variant&lt;int,d=
ouble,std::<wbr>string&gt;;</div><div>=C2=A0 =C2=A0 using Numeric =3D std::=
variant&lt;int,double&gt;;</div><div><br></div><div>=C2=A0 =C2=A0 void func=
(Simple x);</div><div><br></div><div>=C2=A0 =C2=A0 Numeric n;</div><div>=C2=
=A0 =C2=A0 func(n); =C2=A0// Should this work, somehow, since every Numeric=
 &quot;is-a&quot; Simple in some sense?</div></div></blockquote><div><br>Co=
uld it work? Yes. Should it work? Not implicitly; if it should be allowed, =
there ought to be a function for it. When should it work? Well, that&#39;s =
the question.<br>=C2=A0</div><blockquote class=3D"gmail_quote" style=3D"mar=
gin: 0;margin-left: 0.8ex;border-left: 1px #ccc solid;padding-left: 1ex;"><=
div dir=3D"ltr"><div><br></div><div>=C2=A0 =C2=A0 Value v;</div><div>=C2=A0=
 =C2=A0 func(v); =C2=A0// Should this compile, somehow, and then throw at r=
untime if v holds a List or Object?</div></div></blockquote><div><br>No, it=
 should not. Not implicitly.<br><br></div><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></div><div>The &quot;narrowing&quot; case=
 is sort of like dynamic_cast;</div></div></blockquote><div><br>In no sense=
 is it like a dynamic cast. `dynamic_cast` is about casting within a class =
hierarchy. Specifically, from child to parent. The only way it&#39;s simila=
r is that it can conceptually fail. <br><br></div><blockquote class=3D"gmai=
l_quote" style=3D"margin: 0;margin-left: 0.8ex;border-left: 1px #ccc solid;=
padding-left: 1ex;"><div dir=3D"ltr"><div></div><div>Or, is there a clever =
trick to get this kind of behavior already, without having to write an arca=
ne and expensive helper function that does a bunch of get()s and assignment=
s?</div></div></blockquote><div><br>There is nothing arcane about a simple =
visitor:<br><br><div class=3D"prettyprint" style=3D"background-color: rgb(2=
50, 250, 250); border-color: rgb(187, 187, 187); border-style: solid; borde=
r-width: 1px; word-wrap: break-word;"><code class=3D"prettyprint"><div clas=
s=3D"subprettyprint"><span style=3D"color: #008;" class=3D"styled-by-pretti=
fy">template</span><span style=3D"color: #660;" class=3D"styled-by-prettify=
">&lt;</span><span style=3D"color: #008;" class=3D"styled-by-prettify">type=
name</span><span style=3D"color: #000;" class=3D"styled-by-prettify"> </spa=
n><span style=3D"color: #660;" class=3D"styled-by-prettify">...</span><span=
 style=3D"color: #606;" class=3D"styled-by-prettify">Args</span><span style=
=3D"color: #660;" class=3D"styled-by-prettify">&gt;</span><span style=3D"co=
lor: #000;" class=3D"styled-by-prettify"><br>variant_conv<br></span><span s=
tyle=3D"color: #660;" class=3D"styled-by-prettify">{</span><span style=3D"c=
olor: #000;" class=3D"styled-by-prettify"><br>=C2=A0 </span><span style=3D"=
color: #008;" class=3D"styled-by-prettify">template</span><span style=3D"co=
lor: #660;" class=3D"styled-by-prettify">&lt;</span><span style=3D"color: #=
008;" class=3D"styled-by-prettify">typename</span><span style=3D"color: #00=
0;" class=3D"styled-by-prettify"> T</span><span style=3D"color: #660;" clas=
s=3D"styled-by-prettify">&gt;</span><span style=3D"color: #000;" class=3D"s=
tyled-by-prettify"><br>=C2=A0 variant</span><span style=3D"color: #660;" cl=
ass=3D"styled-by-prettify">&lt;</span><span style=3D"color: #606;" class=3D=
"styled-by-prettify">Args</span><span style=3D"color: #660;" class=3D"style=
d-by-prettify">...&gt;</span><span style=3D"color: #000;" class=3D"styled-b=
y-prettify"> </span><span style=3D"color: #008;" class=3D"styled-by-prettif=
y">operator</span><span style=3D"color: #660;" class=3D"styled-by-prettify"=
>()(</span><span style=3D"color: #008;" class=3D"styled-by-prettify">const<=
/span><span style=3D"color: #000;" class=3D"styled-by-prettify"> T </span><=
span style=3D"color: #660;" class=3D"styled-by-prettify">&amp;</span><span =
style=3D"color: #000;" class=3D"styled-by-prettify">t</span><span style=3D"=
color: #660;" class=3D"styled-by-prettify">)</span><span style=3D"color: #0=
00;" class=3D"styled-by-prettify"> </span><span style=3D"color: #008;" clas=
s=3D"styled-by-prettify">const</span><span style=3D"color: #000;" class=3D"=
styled-by-prettify"><br>=C2=A0 </span><span style=3D"color: #660;" class=3D=
"styled-by-prettify">{</span><span style=3D"color: #000;" class=3D"styled-b=
y-prettify"><br>=C2=A0 =C2=A0 </span><span style=3D"color: #800;" class=3D"=
styled-by-prettify">//If T not in Args, throw.</span><span style=3D"color: =
#000;" class=3D"styled-by-prettify"><br>=C2=A0 =C2=A0 </span><span style=3D=
"color: #008;" class=3D"styled-by-prettify">return</span><span style=3D"col=
or: #000;" class=3D"styled-by-prettify"> t</span><span style=3D"color: #660=
;" class=3D"styled-by-prettify">;</span><span style=3D"color: #000;" class=
=3D"styled-by-prettify"><br>=C2=A0 </span><span style=3D"color: #660;" clas=
s=3D"styled-by-prettify">}</span><span style=3D"color: #000;" class=3D"styl=
ed-by-prettify"><br></span><span style=3D"color: #660;" class=3D"styled-by-=
prettify">};</span><span style=3D"color: #000;" class=3D"styled-by-prettify=
"><br><br></span><span style=3D"color: #008;" class=3D"styled-by-prettify">=
auto</span><span style=3D"color: #000;" class=3D"styled-by-prettify"> v </s=
pan><span style=3D"color: #660;" class=3D"styled-by-prettify">=3D</span><sp=
an style=3D"color: #000;" class=3D"styled-by-prettify"> visit</span><span s=
tyle=3D"color: #660;" class=3D"styled-by-prettify">(</span><span style=3D"c=
olor: #000;" class=3D"styled-by-prettify">variant_conv</span><span style=3D=
"color: #660;" class=3D"styled-by-prettify">&lt;</span><span style=3D"color=
: #008;" class=3D"styled-by-prettify">int</span><span style=3D"color: #660;=
" class=3D"styled-by-prettify">,</span><span style=3D"color: #000;" class=
=3D"styled-by-prettify"> </span><span style=3D"color: #008;" class=3D"style=
d-by-prettify">float</span><span style=3D"color: #660;" class=3D"styled-by-=
prettify">,</span><span style=3D"color: #000;" class=3D"styled-by-prettify"=
> std</span><span style=3D"color: #660;" class=3D"styled-by-prettify">::</s=
pan><span style=3D"color: #008;" class=3D"styled-by-prettify">string</span>=
<span style=3D"color: #660;" class=3D"styled-by-prettify">&gt;{},</span><sp=
an style=3D"color: #000;" class=3D"styled-by-prettify"> variant</span><span=
 style=3D"color: #660;" class=3D"styled-by-prettify">&lt;</span><span style=
=3D"color: #008;" class=3D"styled-by-prettify">int</span><span style=3D"col=
or: #660;" class=3D"styled-by-prettify">,</span><span style=3D"color: #000;=
" class=3D"styled-by-prettify"> </span><span style=3D"color: #008;" class=
=3D"styled-by-prettify">float</span><span style=3D"color: #660;" class=3D"s=
tyled-by-prettify">&gt;{</span><span style=3D"color: #066;" class=3D"styled=
-by-prettify">3.4f</span><span style=3D"color: #660;" class=3D"styled-by-pr=
ettify">});</span></div></code></div><br>You can even wrap that up in a tem=
plate function.<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&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_15_2090865752.1446160224012--
------=_Part_14_1132156555.1446160224012--

.
