220 34718 <03509464-1e39-4d2f-ba26-c192191e72e6@isocpp.org> article
Path: news.gmane.org!.POSTED!not-for-mail
From: Mingxin Wang <wmx16835vv@163.com>
Newsgroups: gmane.comp.lang.c++.isocpp.proposals
Subject: Re: Extensions for Disambiguation Tags
Date: Mon, 9 Oct 2017 08:48:01 -0700 (PDT)
Lines: 222
Approved: news@gmane.org
Message-ID: <03509464-1e39-4d2f-ba26-c192191e72e6@isocpp.org>
References: <527c448b-b8dd-4877-b63f-b399c167da4d@isocpp.org>
 <20171009040212.5161043.12800.37351@gmail.com>
Reply-To: std-proposals@isocpp.org
NNTP-Posting-Host: blaine.gmane.org
Mime-Version: 1.0
Content-Type: multipart/mixed; 
	boundary="----=_Part_2745_1648152961.1507564081963"
X-Trace: blaine.gmane.org 1507564105 32499 195.159.176.226 (9 Oct 2017 15:48:25 GMT)
X-Complaints-To: usenet@blaine.gmane.org
NNTP-Posting-Date: Mon, 9 Oct 2017 15:48:25 +0000 (UTC)
To: ISO C++ Standard - Future Proposals <std-proposals@isocpp.org>
Original-X-From: std-proposals+bncBDNMBNHJWIGBBMVU53HAKGQEKCOFSQQ@isocpp.org Mon Oct 09 17:48:18 2017
Return-path: <std-proposals+bncBDNMBNHJWIGBBMVU53HAKGQEKCOFSQQ@isocpp.org>
Envelope-to: gclcip-std-proposals@m.gmane.org
Original-Received: from mail-io0-f197.google.com ([209.85.223.197])
	by blaine.gmane.org with esmtp (Exim 4.84_2)
	(envelope-from <std-proposals+bncBDNMBNHJWIGBBMVU53HAKGQEKCOFSQQ@isocpp.org>)
	id 1e1aHm-0006PA-00
	for gclcip-std-proposals@m.gmane.org; Mon, 09 Oct 2017 17:48:06 +0200
Original-Received: by mail-io0-f197.google.com with SMTP id b186sf6122362iof.21
        for <gclcip-std-proposals@m.gmane.org>; Mon, 09 Oct 2017 08:48:13 -0700 (PDT)
DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed;
        d=isocpp-org.20150623.gappssmtp.com; s=20150623;
        h=date:from:to:message-id:in-reply-to:references:subject:mime-version
         :x-original-sender:reply-to:precedence:mailing-list:list-id
         :list-post:list-help:list-archive:list-subscribe:list-unsubscribe;
        bh=5OJWOdV1xuqEkY5SHNUSlh2oFqxCe4/epr5ljLB1r0s=;
        b=MfWXGTq1PZ5FnTHeCK37MdNIrgDB65le3KrPNR3RimtFGfSn/0YbSSV2TW4xdM8TCm
         t80/8iA5aPVhqtz74v1606EbUEoTi+t/wylBBTFlbcUPqSoKJAG480IutdqCdKZG/jwa
         ZSaPY6hKdjwAy7SFe9jUBks+cMbc5tmuIzpf/h6etBCKtJ8y3BQ4q3kI0NjA3eHOkEjK
         lTStpyYlMG5CJWNxbYzwC+ydUKT095mBfRisFouZbyymHgy5S7Fjiy+3/tne83OdhPyE
         srCopadmwqqecw/aICnVQAtACLSy6L+cXF2v0T137KFeavSzbTPfwZxr0mHfSuEnn4QF
         MhPg==
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: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=5OJWOdV1xuqEkY5SHNUSlh2oFqxCe4/epr5ljLB1r0s=;
        b=LqY3fK/XKPZx8ulqcITB5aedN9NvoIlfidGrCq2oEwpjPe1gojBmvdDHSxysTKF1dm
         xKRznNzzk8JhtL5352z/3nA+e7VcB0eS2iXne+5qb56yYkUmi23H18OBNiBJS184IEa3
         HOHcF4GbbYltzRh87MuSCTouj98XNIGBE8UI5Bi69w5hlApBDBuVcFcgdtb6B+7OGjSb
         KaOjlgVdVdJNIrSo06LrKVVczoheMQUy0simfZaryqtkB1jWwFIIDpLhGoRuUYrskX8Y
         jeWTe9/deKLQRgVINUUQNWe/jVZr5p+sXWIrDpZeVMlwNjTInOf+OwCBcpBfrvQdQO8+
         0pPg==
X-Gm-Message-State: AMCzsaWt9BIU76uusxeFvSt6T/hCJWvTRUP2i2skQ8/3c820E96UCCFg
	W3L8t6YI2T2BsdNcyOVD0jPmUg==
X-Google-Smtp-Source: AOwi7QApRYjldhNbZ96gItj/eWdzQc6XC1HiPEAi5xG3zGHAMUElchWOq8c2AJv+uoTZNwdRtdbQPg==
X-Received: by 10.31.7.81 with SMTP id 78mr1445232vkh.42.1507564093212;
        Mon, 09 Oct 2017 08:48:13 -0700 (PDT)
X-BeenThere: std-proposals@isocpp.org
Original-Received: by 10.31.60.206 with SMTP id j197ls1229802vka.8.gmail; Mon, 09 Oct
 2017 08:48:02 -0700 (PDT)
X-Received: by 10.31.153.200 with SMTP id b191mr471199vke.28.1507564082559;
        Mon, 09 Oct 2017 08:48:02 -0700 (PDT)
In-Reply-To: <20171009040212.5161043.12800.37351@gmail.com>
X-Original-Sender: wmx16835vv@163.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: <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:34718
Archived-At: <http://permalink.gmane.org/gmane.comp.lang.c++.isocpp.proposals/34718>

------=_Part_2745_1648152961.1507564081963
Content-Type: multipart/alternative; 
	boundary="----=_Part_2746_1907148332.1507564081963"

------=_Part_2746_1907148332.1507564081963
Content-Type: text/plain; charset="UTF-8"

With the support of `in_place_arg`, it becomes easy to pass any constexpr 
value (providing its type is valid for a template non-type parameter) to a 
function template using a uniform disambiguation tag, especially with 
constructors.

Providing there is a enum class defined as follows:

enum class State {
  RUNNINE, AVAILABLE, OFFLINE
};

And there is a constexpr function that could convert a `State` to its 
string expression:

constexpr const char* make_state_str(State s) {
  switch (s) {
    case State::RUNNINE: return "Running State";
    case State::AVAILABLE: return "Available State";
    case State::OFFLINE: return "Offline State";
    default: return "Unknown State";
  }
}

It is relatively easy to design a class with `in_place_arg`, which is 
explicitly constructible from a `constexpr State` and stores its string 
expression without executing the constexpr function `make_state_str` at 
runtime:

class Machine {
 public:
  template <State S>
  explicit Machine(std::in_place_arg_t<State, S>) : 
state_str_(STATE_STR<S>) {}

  const char* get_state_str() { return state_str_; }

 private:
  const char* state_str_;

  template <State S>
  static constexpr const char* STATE_STR = make_state_str(S);
};

Machine machine(std::in_place_arg<State, State::AVAILABLE>);
puts(machine.get_state_str());

`in_place_resource` has a wider scope of application than `in_place_arg` 
does, because it could carry all sort of constexpr data if the data is 
prior declared.

For example, providing there is a struct carries some configuration:

struct Config {
  double EPS = 1e-8;
  double PI = acos(-1);
  int INF = 0x7f7f7f7f;
  long long INFL = 0x7f7f7f7f7f7f7f7fL;
} constexpr MATH_CONFIG;

It is allowed to initialize a type with the resource by templates with 
`in_place_resource`, even if we are not sure about the concrete type of the 
resource:

class Calculator {
 public:
  template <class T, const T& CONFIG>
  explicit Calculator(std::in_place_resource_t<T, CONFIG>);
};

Calculator calculator(std::in_place_resource<Config, MATH_CONFIG>);

Actually, `in_place_resource` could also be used to improve the 
implementation in my earlier posts about polymorphism, which used to have 
bottleneck on the size (thanks to Nicol Bolas). With the support of 
`in_place_resource`, the bottleneck no longer exist, because it becomes 
possible to merge the virtual table with the lifetime management functions 
at compile time.

Mingxin Wang


On Monday, October 9, 2017 at 12:02:15 PM UTC+8, Tony V E wrote:
>
> How about some examples showing usage?
>
> Sent from my BlackBerry portable Babbage Device
>

-- 
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/03509464-1e39-4d2f-ba26-c192191e72e6%40isocpp.org.

------=_Part_2746_1907148332.1507564081963
Content-Type: text/html; charset="UTF-8"
Content-Transfer-Encoding: quoted-printable

<div dir=3D"ltr"><div>With the support of `in_place_arg`, it becomes easy t=
o pass any constexpr value (providing its type is valid for a template non-=
type parameter) to a function template using a uniform disambiguation tag, =
especially with constructors.</div><div><br></div><div>Providing there is a=
 enum class defined as follows:</div><div><br></div><div><div class=3D"pret=
typrint" style=3D"background-color: rgb(250, 250, 250); border-color: rgb(1=
87, 187, 187); border-style: solid; border-width: 1px; word-wrap: break-wor=
d;"><code class=3D"prettyprint"><div class=3D"subprettyprint"><div class=3D=
"subprettyprint">enum class State {</div><div class=3D"subprettyprint">=C2=
=A0 RUNNINE, AVAILABLE, OFFLINE</div><div class=3D"subprettyprint">};</div>=
</div></code></div><br>And there is a constexpr function that could convert=
 a `State` to its string expression:<br></div><div><br></div><div><div clas=
s=3D"prettyprint" style=3D"background-color: rgb(250, 250, 250); border-col=
or: rgb(187, 187, 187); border-style: solid; border-width: 1px; word-wrap: =
break-word;"><code class=3D"prettyprint"><div class=3D"subprettyprint"><div=
 class=3D"subprettyprint">constexpr const char* make_state_str(State s) {</=
div><div class=3D"subprettyprint">=C2=A0 switch (s) {</div><div class=3D"su=
bprettyprint">=C2=A0 =C2=A0 case State::RUNNINE: return &quot;Running State=
&quot;;</div><div class=3D"subprettyprint">=C2=A0 =C2=A0 case State::AVAILA=
BLE: return &quot;Available State&quot;;</div><div class=3D"subprettyprint"=
>=C2=A0 =C2=A0 case State::OFFLINE: return &quot;Offline State&quot;;</div>=
<div class=3D"subprettyprint">=C2=A0 =C2=A0 default: return &quot;Unknown S=
tate&quot;;</div><div class=3D"subprettyprint">=C2=A0 }</div><div class=3D"=
subprettyprint">}</div></div></code></div><div><br></div>It is relatively e=
asy to design a class with `in_place_arg`, which is explicitly constructibl=
e from a `constexpr State` and stores its string expression without executi=
ng the constexpr function `make_state_str` at runtime:<br><br></div><div><d=
iv class=3D"prettyprint" style=3D"background-color: rgb(250, 250, 250); bor=
der-color: rgb(187, 187, 187); border-style: solid; border-width: 1px; word=
-wrap: break-word;"><code class=3D"prettyprint"><div class=3D"subprettyprin=
t"><font color=3D"#660066"><div class=3D"subprettyprint">class Machine {</d=
iv><div class=3D"subprettyprint">=C2=A0public:</div><div class=3D"subpretty=
print">=C2=A0 template &lt;State S&gt;</div><div class=3D"subprettyprint">=
=C2=A0 explicit Machine(std::in_place_arg_t&lt;State, S&gt;) : state_str_(S=
TATE_STR&lt;S&gt;) {}</div><div class=3D"subprettyprint"><br></div><div cla=
ss=3D"subprettyprint">=C2=A0 const char* get_state_str() { return state_str=
_; }</div><div class=3D"subprettyprint"><br></div><div class=3D"subprettypr=
int">=C2=A0private:</div><div class=3D"subprettyprint">=C2=A0 const char* s=
tate_str_;</div><div class=3D"subprettyprint"><br></div><div class=3D"subpr=
ettyprint">=C2=A0 template &lt;State S&gt;</div><div class=3D"subprettyprin=
t">=C2=A0 static constexpr const char* STATE_STR =3D make_state_str(S);</di=
v><div class=3D"subprettyprint">};</div><div class=3D"subprettyprint"><br><=
/div><div class=3D"subprettyprint">Machine machine(std::in_place_arg&lt;Sta=
te, State::AVAILABLE&gt;);</div><div class=3D"subprettyprint">puts(machine.=
get_state_str());</div></font></div></code></div><br><div>`in_place_resourc=
e` has a wider scope of application than `in_place_arg` does, because it co=
uld carry all sort of constexpr data if the data is prior declared.</div><d=
iv><br></div><div>For example, providing there is a struct carries some con=
figuration:</div></div><div><br></div><div><div class=3D"prettyprint" style=
=3D"background-color: rgb(250, 250, 250); border-color: rgb(187, 187, 187);=
 border-style: solid; border-width: 1px; word-wrap: break-word;"><code clas=
s=3D"prettyprint"><div class=3D"subprettyprint"><div class=3D"subprettyprin=
t">struct Config {</div><div class=3D"subprettyprint">=C2=A0 double EPS =3D=
 1e-8;</div><div class=3D"subprettyprint">=C2=A0 double PI =3D acos(-1);</d=
iv><div class=3D"subprettyprint">=C2=A0 int INF =3D 0x7f7f7f7f;</div><div c=
lass=3D"subprettyprint">=C2=A0 long long INFL =3D 0x7f7f7f7f7f7f7f7fL;</div=
><div class=3D"subprettyprint">} constexpr MATH_CONFIG;</div></div></code><=
/div><br>It is allowed to initialize a type with the resource by templates =
with `in_place_resource`, even if we are not sure about the concrete type o=
f the resource:<br></div><div><br></div><div><div class=3D"prettyprint" sty=
le=3D"background-color: rgb(250, 250, 250); border-color: rgb(187, 187, 187=
); border-style: solid; border-width: 1px; word-wrap: break-word;"><code cl=
ass=3D"prettyprint"><div class=3D"subprettyprint"><font color=3D"#660066"><=
div class=3D"subprettyprint">class Calculator {</div><div class=3D"subprett=
yprint">=C2=A0public:</div><div class=3D"subprettyprint">=C2=A0 template &l=
t;class T, const T&amp; CONFIG&gt;</div><div class=3D"subprettyprint">=C2=
=A0 explicit Calculator(std::in_place_resource_t&lt;T, CONFIG&gt;);</div><d=
iv class=3D"subprettyprint">};</div><div class=3D"subprettyprint"><br></div=
><div class=3D"subprettyprint">Calculator calculator(std::in_place_resource=
&lt;Config, MATH_CONFIG&gt;);</div></font></div></code></div><br></div><div=
><div>Actually, `in_place_resource` could also be used to improve the imple=
mentation in my earlier posts about polymorphism, which used to have bottle=
neck on the size (thanks to Nicol Bolas). With the support of `in_place_res=
ource`, the bottleneck no longer exist, because it becomes possible to merg=
e the virtual table with the lifetime management functions at compile time.=
</div><div><br></div><div>Mingxin Wang</div></div><div><br></div><br>On Mon=
day, October 9, 2017 at 12:02:15 PM UTC+8, Tony V E wrote:<blockquote class=
=3D"gmail_quote" style=3D"margin: 0;margin-left: 0.8ex;border-left: 1px #cc=
c solid;padding-left: 1ex;"><div lang=3D"en-US" style=3D"background-color:r=
gb(255,255,255);line-height:initial">                                      =
                                                <div style=3D"width:100%;fo=
nt-size:initial;font-family:Calibri,&#39;Slate Pro&#39;,sans-serif,sans-ser=
if;color:rgb(31,73,125);text-align:initial;background-color:rgb(255,255,255=
)">How about some examples showing usage?</div>                            =
                                                                           =
                              <div style=3D"width:100%;font-size:initial;fo=
nt-family:Calibri,&#39;Slate Pro&#39;,sans-serif,sans-serif;color:rgb(31,73=
,125);text-align:initial;background-color:rgb(255,255,255)"><br style=3D"di=
splay:initial"></div>                                                      =
                                                                           =
                                                                  <div styl=
e=3D"font-size:initial;font-family:Calibri,&#39;Slate Pro&#39;,sans-serif,s=
ans-serif;color:rgb(31,73,125);text-align:initial;background-color:rgb(255,=
255,255)">Sent=C2=A0from=C2=A0my=C2=A0BlackBerry=C2=A0<wbr>portable=C2=A0Ba=
bbage=C2=A0Device</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/03509464-1e39-4d2f-ba26-c192191e72e6%=
40isocpp.org?utm_medium=3Demail&utm_source=3Dfooter">https://groups.google.=
com/a/isocpp.org/d/msgid/std-proposals/03509464-1e39-4d2f-ba26-c192191e72e6=
%40isocpp.org</a>.<br />

------=_Part_2746_1907148332.1507564081963--

------=_Part_2745_1648152961.1507564081963--

.
