220 38143 <1bc6ebbe-4d52-40b7-9e06-d096e76631ba@isocpp.org> article
Path: news.gmane.org!.POSTED!not-for-mail
From: Raymund Hofmann <hofmannraymund@gmail.com>
Newsgroups: gmane.comp.lang.c++.isocpp.proposals
Subject: Re: Better DRY when initializing members in constructor
Date: Mon, 21 May 2018 00:23:18 -0700 (PDT)
Lines: 120
Approved: news@gmane.org
Message-ID: <1bc6ebbe-4d52-40b7-9e06-d096e76631ba@isocpp.org>
References: <cd751fc2-11ab-4e7c-b90e-5045f7df2c5c@isocpp.org>
 <20180521070748.GA3811@noemi.bahnhof.se>
Reply-To: std-proposals@isocpp.org
NNTP-Posting-Host: blaine.gmane.org
Mime-Version: 1.0
Content-Type: multipart/mixed; 
	boundary="----=_Part_26791_883090159.1526887398883"
X-Trace: blaine.gmane.org 1526887274 24584 195.159.176.226 (21 May 2018 07:21:14 GMT)
X-Complaints-To: usenet@blaine.gmane.org
NNTP-Posting-Date: Mon, 21 May 2018 07:21:14 +0000 (UTC)
Cc: hofmannraymund@gmail.com
To: ISO C++ Standard - Future Proposals <std-proposals@isocpp.org>
Original-X-From: std-proposals+bncBCHZVUUFSEJRBZ7HRHMAKGQERUPC73Y@isocpp.org Mon May 21 09:21:10 2018
Return-path: <std-proposals+bncBCHZVUUFSEJRBZ7HRHMAKGQERUPC73Y@isocpp.org>
Envelope-to: gclcip-std-proposals@m.gmane.org
Original-Received: from mail-qk0-f200.google.com ([209.85.220.200])
	by blaine.gmane.org with esmtp (Exim 4.84_2)
	(envelope-from <std-proposals+bncBCHZVUUFSEJRBZ7HRHMAKGQERUPC73Y@isocpp.org>)
	id 1fKf81-0006HR-DW
	for gclcip-std-proposals@m.gmane.org; Mon, 21 May 2018 09:21:09 +0200
Original-Received: by mail-qk0-f200.google.com with SMTP id c73-v6sf14326513qke.2
        for <gclcip-std-proposals@m.gmane.org>; Mon, 21 May 2018 00:23:21 -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:list-post:list-help:list-archive:list-subscribe
         :list-unsubscribe;
        bh=IFEvKJP3xmMcKjfJcE+cArTUHRv1krV3WsagPZwT5mg=;
        b=uISUGz66H7+HTmhZ3HNbgA24owvutAuhDDauSwqJ3he8GMmprJNgPzklCBh0JqSZM0
         mCdv74mGfmAz3AD5yu5DfcpgdsBO5efGKmoTyXfH9btEK0o7T3bkQqLV7nJ5INTlNxs0
         HTK9rWe1kTof59ccvGddxIiJ6vFBtI9Ogr7ilxl7Js8mA9nhzTg/kHb3tS6Exz5W5oJf
         zEXs0c91MUChfP1h/j//k5bdB8uTU4s0KhCENuScagkvsIobJx/4UxWxxvej0GKuUFGU
         APkUue/tR6jeItvx9UOSlswzqMk5WSC57jGITXlRGmD955i02eRX+eKsYj3w2+EuNoye
         iyAw==
DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed;
        d=gmail.com; s=20161025;
        h=date:from:to:cc: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=IFEvKJP3xmMcKjfJcE+cArTUHRv1krV3WsagPZwT5mg=;
        b=HIjlpynbCYFZoSf8eZ8byFHsp44jhqlqUe5SEIlqA9+itDxdHF4mc8+ieDXvsZwYMN
         muaFV7o3uoyhP4h+6g15KQc+jeWm3Dvyr99LMlb3dbI7QyzLRWy5Zku2GtnTGtxBEg0z
         ZZ1UoBN1jvWweMP9LWEPT5qbgQh7258yDJFNYklEanREQg8kOXahNDrX8kyxIZSxxJHR
         Gsy8lNcTzbVSAleU3LaGkwOYq5G4uyYDg+V6O5HJqiV1mRbWCzkqWuuF/57mw72IwfXJ
         Gy95JYcwzt754qRWwOuX2BrCJK0AH305afEO9/QX4VVqdcY1Cjzkl+J+H7E60S86uaOs
         3fPg==
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=IFEvKJP3xmMcKjfJcE+cArTUHRv1krV3WsagPZwT5mg=;
        b=WEfuAPd3T3Z1QVSjRRJJqKYSXmL1C7oDkJdcJCRqglFMAHa7E/9cu74qsfOHA0cZYy
         0RpT4QIeVc3jmPDivjeQ+zzTJvDl38VplEpUyCuqJF6afR0oQvWr3Cc3iqJKezvTbcZi
         KS/WOs5RWQGlkbR83vqbfGWCgj7KCHlJuuHnswMu2bT7KrQ0C4YpbbZd0qmk14bC47ca
         +6uGDN/2fjdz3Ku7LIGP2/w7kM/oqtk2PUxfiFUMwhJ5ww5RELr8bPPZVqxjowgYDWFF
         C2N9tNaRUXDk7PiTsfRzYTW2L/NfoO7r0NaXwHoVbPmJLmdegUMpud4ZawK0siut6qxV
         x1Aw==
X-Gm-Message-State: ALKqPwepDhlhjnhP/2Ym+R1IZcpwQprIjd5FlxGKgusfEXJdHKhrCGW5
	VvZf0M8J/KkBWGnoaxtjIlEWTA==
X-Google-Smtp-Source: AB8JxZqtKdRIi3mc12aJL2Wskk91H3qOm+NF14probQt/pB8wM9qNvyZsYWK2L0V9eQsAEC+2bMl+w==
X-Received: by 2002:aed:3d76:: with SMTP id h51-v6mr12054576qtf.35.1526887400526;
        Mon, 21 May 2018 00:23:20 -0700 (PDT)
X-BeenThere: std-proposals@isocpp.org
Original-Received: by 2002:ac8:22d7:: with SMTP id g23-v6ls6817304qta.10.gmail; Mon, 21
 May 2018 00:23:19 -0700 (PDT)
X-Received: by 2002:ac8:7293:: with SMTP id v19-v6mr1327985qto.0.1526887399306;
        Mon, 21 May 2018 00:23:19 -0700 (PDT)
In-Reply-To: <20180521070748.GA3811@noemi.bahnhof.se>
X-Original-Sender: hofmannraymund@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: <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:38143
Archived-At: <http://permalink.gmane.org/gmane.comp.lang.c++.isocpp.proposals/38143>

------=_Part_26791_883090159.1526887398883
Content-Type: multipart/alternative; 
	boundary="----=_Part_26792_1421355074.1526887398883"

------=_Part_26792_1421355074.1526887398883
Content-Type: text/plain; charset="UTF-8"

I am aware of that, In my example you can see i already used what you 
propose in a slightly more indirect way, because it has limitations:

struct T
{
    struct I
    {
        int i;
        const char* cp;
    } m;
    T(const I& i) : m(i) {}
};

usage:

void testf()
{
    T t({ 1,"abc" });
}

I chose this way because if you inherit for example from std::
enable_shared_from_this, you lose the ability to initialize members in the 
way you have shown, you are required to have a constructor then:

struct TS : std::enable_shared_from_this<T>
{
    struct I
    {
        int i;
        const char* cp;
    } m;
    //TS(const I& i) : m(i) {}
};

TS ts{ 1,"abc" }; // error

even worse, when you create a object with make_shared like so:

struct TS : std::enable_shared_from_this<T>
{
    struct I
    {
        int i;
        const char* cp;
    } m;
    TS(const I& i) : m(i) {}
};

void testf()
{
    auto so = std::make_shared<T>(TS::I{ 1,"abc" });
}

You have to repeat I defined in TS, or alternatively write a constructor 
which needs too much repetition as mentioned.


-- 
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/1bc6ebbe-4d52-40b7-9e06-d096e76631ba%40isocpp.org.

------=_Part_26792_1421355074.1526887398883
Content-Type: text/html; charset="UTF-8"
Content-Transfer-Encoding: quoted-printable

<div dir=3D"ltr"><span style=3D"font-family: arial, sans-serif;">I am aware=
 of that, In my example you can see i already used what you propose in a sl=
ightly more indirect way, because it has limitations:<br><br><span>struct T=
<br>{<br>=C2=A0=C2=A0=C2=A0 struct I<br>=C2=A0=C2=A0=C2=A0 {<br>=C2=A0=C2=
=A0=C2=A0 =C2=A0=C2=A0=C2=A0 int i;<br>=C2=A0=C2=A0=C2=A0 =C2=A0=C2=A0=C2=
=A0 const char* cp;<br>=C2=A0=C2=A0=C2=A0 } m;<br>=C2=A0=C2=A0=C2=A0 T(cons=
t I&amp; i) : m(i) {}<br>};<br><br></span>usage:<br><br><span>void testf()<=
br>{<br>=C2=A0=C2=A0=C2=A0 T t({ 1,&quot;abc&quot; });<br>}<br><br>I chose =
this way because if you inherit for example from</span><span style=3D"font-=
size:0.7em; line-height:130%"> std::</span>enable_shared_from_this<span>, y=
ou lose the ability to initialize members in the way you have shown, you ar=
e required to have a constructor then:<br><br>struct TS : std::enable_share=
d_from_this&lt;T&gt;<br>{<br>=C2=A0=C2=A0=C2=A0 struct I<br>=C2=A0=C2=A0=C2=
=A0 {<br>=C2=A0=C2=A0=C2=A0 =C2=A0=C2=A0=C2=A0 int i;<br>=C2=A0=C2=A0=C2=A0=
 =C2=A0=C2=A0=C2=A0 const char* cp;<br>=C2=A0=C2=A0=C2=A0 } m;<br>=C2=A0=C2=
=A0=C2=A0 //TS(const I&amp; i) : m(i) {}<br>};<br><br>TS ts{ 1,&quot;abc&qu=
ot; }; // error<br><br>even worse, when you create a object with make_share=
d like so:<br><br>struct TS : std::enable_shared_from_this&lt;T&gt;<br>{<br=
>=C2=A0=C2=A0=C2=A0 struct I<br>=C2=A0=C2=A0=C2=A0 {<br>=C2=A0=C2=A0=C2=A0 =
=C2=A0=C2=A0=C2=A0 int i;<br>=C2=A0=C2=A0=C2=A0 =C2=A0=C2=A0=C2=A0 const ch=
ar* cp;<br>=C2=A0=C2=A0=C2=A0 } m;<br>=C2=A0=C2=A0=C2=A0 TS(const I&amp; i)=
 : m(i) {}<br>};<br><br>void testf()<br>{<br>=C2=A0=C2=A0=C2=A0 auto so =3D=
 std::make_shared&lt;T&gt;(TS::I{ 1,&quot;abc&quot; });<br>}<br><br>You hav=
e to repeat I defined in TS, or alternatively write a constructor which nee=
ds too much repetition as mentioned.<br><br><br></span></span></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/1bc6ebbe-4d52-40b7-9e06-d096e76631ba%=
40isocpp.org?utm_medium=3Demail&utm_source=3Dfooter">https://groups.google.=
com/a/isocpp.org/d/msgid/std-proposals/1bc6ebbe-4d52-40b7-9e06-d096e76631ba=
%40isocpp.org</a>.<br />

------=_Part_26792_1421355074.1526887398883--

------=_Part_26791_883090159.1526887398883--

.
