220 38142 <20180521070748.GA3811@noemi.bahnhof.se> article
Path: news.gmane.org!.POSTED!not-for-mail
From: Magnus Fromreide <magfr@lysator.liu.se>
Newsgroups: gmane.comp.lang.c++.isocpp.proposals
Subject: Re: Better DRY when initializing members in constructor
Date: Mon, 21 May 2018 09:07:48 +0200
Lines: 83
Approved: news@gmane.org
Message-ID: <20180521070748.GA3811@noemi.bahnhof.se>
References: <cd751fc2-11ab-4e7c-b90e-5045f7df2c5c@isocpp.org>
Reply-To: std-proposals@isocpp.org
NNTP-Posting-Host: blaine.gmane.org
Mime-Version: 1.0
Content-Type: text/plain; charset="UTF-8"
X-Trace: blaine.gmane.org 1526886349 24329 195.159.176.226 (21 May 2018 07:05:49 GMT)
X-Complaints-To: usenet@blaine.gmane.org
NNTP-Posting-Date: Mon, 21 May 2018 07:05:49 +0000 (UTC)
User-Agent: Mutt/1.9.2 (2017-12-15)
To: std-proposals@isocpp.org, Raymund Hofmann <hofmannraymund@gmail.com>
Original-X-From: std-proposals+bncBDELLREETMBRBR7ARHMAKGQERWGMHSI@isocpp.org Mon May 21 09:05:45 2018
Return-path: <std-proposals+bncBDELLREETMBRBR7ARHMAKGQERWGMHSI@isocpp.org>
Envelope-to: gclcip-std-proposals@m.gmane.org
Original-Received: from mail-lf0-f72.google.com ([209.85.215.72])
	by blaine.gmane.org with esmtp (Exim 4.84_2)
	(envelope-from <std-proposals+bncBDELLREETMBRBR7ARHMAKGQERWGMHSI@isocpp.org>)
	id 1fKet3-0006Bq-HA
	for gclcip-std-proposals@m.gmane.org; Mon, 21 May 2018 09:05:41 +0200
Original-Received: by mail-lf0-f72.google.com with SMTP id u87-v6sf5307941lfi.2
        for <gclcip-std-proposals@m.gmane.org>; Mon, 21 May 2018 00:07:53 -0700 (PDT)
ARC-Seal: i=2; a=rsa-sha256; t=1526886472; cv=pass;
        d=google.com; s=arc-20160816;
        b=zNdlFZ+Gy0f2S7tvwNb1s2Ci3gJt+EuxVmgMTZuwfaAjmDtqNpyUJlpNBLdtY3+aQx
         3FBArX56fpLn32VXQBFprrysaPoVw+MaT5vEhQKzEaAW9Zf8v7KLA/EeLNzBxygyMgNs
         8PGuY63SHNfnA2y0Y7luB2P314rdFY2SMGVqxw700/7kBhnjWXob94d8/vWg+wRQyhhJ
         TayQBPzBNquvYID3hVpbCMk2ZNru3VAwasT0AQkbb3G3nZ2BE59Tgdq6nSU0FUp3gDhd
         Kumxkc7wlP0fH7rysbzVwtM09kd+V5j1kWm5rwClqfiP2lamLIouqatrXdRbTgYgfXk/
         wo8Q==
ARC-Message-Signature: i=2; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816;
        h=list-unsubscribe:list-subscribe:list-archive:list-help:list-post
         :list-id:mailing-list:precedence:reply-to:user-agent:in-reply-to
         :content-disposition:mime-version:references:mail-followup-to
         :message-id:subject:to:from:date:arc-authentication-results
         :arc-message-signature:dkim-signature:arc-authentication-results;
        bh=9f+FBnzdelviuNZSaO7HEizQDsz+pY91FaDOxQ4YlAs=;
        b=ldfDBeoibXUHIUYKTIHkEKA+ZeLZdF9+GyTCwXnEGO5SACVjFa46a/SmCLIKI6CpaO
         k/gS1A9x+MRUZdcYZ4mUMqUY+d7RyoxVLEtkq4aSeR4klEzLZOyLNgLKlObnr9ED4MRh
         i9flzVvZ7wIVuDUgDjZ5xJun+PROFfeczIQWU6iyn0vlh8aUB7wspG8CSLfwYzkX9xVh
         I+wo70c2uL5zPvp8LsniNwhNw27k5i7vcRmD39zninjh9Fp7rRueDoPCxFR5InESUgPz
         Tn/lRmLFdI9J5qWFcuCJULnwYDylCg+zVR0O7LYNEv5cPpz/mwoFzDXwe5wr+/wN2/KG 
ARC-Authentication-Results: i=2; mx.google.com;
       spf=pass (google.com: domain of magfr@lysator.liu.se designates 130.236.254.3 as permitted sender) smtp.mailfrom=magfr@lysator.liu.se
DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed;
        d=isocpp-org.20150623.gappssmtp.com; s=20150623;
        h=date:from:to:subject:message-id:mail-followup-to:references
         :mime-version:content-disposition:in-reply-to:user-agent
         :x-original-sender:x-original-authentication-results:reply-to
         :precedence:mailing-list:list-id:list-post:list-help:list-archive
         :list-subscribe:list-unsubscribe;
        bh=9f+FBnzdelviuNZSaO7HEizQDsz+pY91FaDOxQ4YlAs=;
        b=wGTYsl10+yh6k2vxftfxCto2iYVSRP07rANeM5WaT9myDXvNtbefWuIkFhXcYob+lX
         dIGfOCV+TPviXDTx5pJkI5E6QE9Tyb2rwBCnIBKT7liSyTAPtRxqxpXni6qbZ0QZAuJT
         qs+6WxL8G6gD/W36LGHmRto8jXQDT/+1UZq0neLmh0fJ5auYUfB9iF1AtTaQSWxwy6yi
         EvUnCcB3SmyiXhYxcJOY4t0qTq3tm5dF+p0b/MDVjaQOluL5csANbuIjHrpBVDeDKv19
         AH2WrndTxLZsMPHmEiHZBVctw+1ySpQDPfl06B7scjdK5vR24qXGJci0ob5pZBK1+c7e
         rgdQ==
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:subject:message-id:mail-followup-to
         :references:mime-version:content-disposition:in-reply-to:user-agent
         :x-original-sender:x-original-authentication-results:reply-to
         :precedence:mailing-list:list-id:x-spam-checked-in-group:list-post
         :list-help:list-archive:list-subscribe:list-unsubscribe;
        bh=9f+FBnzdelviuNZSaO7HEizQDsz+pY91FaDOxQ4YlAs=;
        b=uPE3WYOuic7cpOAbOUMz91hQZBgiBI1n8CaahL/afK7vPh4pwzgOE4PXqpOeUoxQsD
         PWPl2WyuAw5pkMubn933yb19oucGycRHx8/9I1oME3KgazMBOAagcryvwvrxg8KBJdwz
         kkeeRZ16DoBGs4QG+E93Mou8cPp0vZl4ZWReF5wAeNSoUFo0oj2+6ijKXCEeUfnooash
         Be6U7AiThJkfUaztSyRqZlgV9o65Fq/F7e0Nz8oeSveVRJW5FazEWWNmrj6ndpl+YlWO
         FhayBvoqYN7gnIOxwhi7Ic4FO9uMD7iMNZaG9nIEgZ3jzHHz3G4Gf7hvC15 
X-Gm-Message-State: ALKqPwcH9HXEF9gfeaDYIZew2Yv9Hp2LzMnT6r4rXaQXKtQmfnQH77TA
	xYFsii6y0P+/POl5oc29NDjMuQ==
X-Google-Smtp-Source: AB8JxZqXGblaimXKyGiuggwaLLpW8Wr4ahwVAg394VqS7GzDPiPXtkQOgu0Us2dys6oGjYahyiFD/w==
X-Received: by 2002:a2e:7306:: with SMTP id o6-v6mr1007219ljc.30.1526886472880;
        Mon, 21 May 2018 00:07:52 -0700 (PDT)
X-BeenThere: std-proposals@isocpp.org
Original-Received: by 2002:a2e:95c1:: with SMTP id y1-v6ls1423081ljh.13.gmail; Mon, 21
 May 2018 00:07:51 -0700 (PDT)
X-Received: by 2002:a2e:2f07:: with SMTP id v7-v6mr11858771ljv.113.1526886471301;
        Mon, 21 May 2018 00:07:51 -0700 (PDT)
ARC-Seal: i=1; a=rsa-sha256; t=1526886471; cv=none;
        d=google.com; s=arc-20160816;
        b=IJV5rvs/+v7s7NKz2CcfTWiLIJ1mwnoBPAYmjSCLZX6dcSqq+k8YuWfJbsalUlEcGB
         6och1Oypu/pOzan35rYvbYLOgI3HecjT0VLDt0oMNcMs1bK2qiUSAkbMly4TzpkdQEYg
         vdBCiKQwQLrn5kwVxbfzBEqaYLJeYGzacdhy/k2tSGh8dfx7pouXEl0uu24nrB31dtOb
         0XxLichL1Ugj4BJpaaG89PVgm0WXzT6431nYOFydI3FXFBZ7Lp4Uzb7uzhU8BTQwsHKc
         ZkVArKt1CXoN2sggqhXZrNuR8TBNExtOnDbheYgVJ8hC71AD25/vMbyI+OHYvHXA37iz
         YNCQ==
ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816;
        h=user-agent:in-reply-to:content-disposition:mime-version:references
         :mail-followup-to:message-id:subject:to:from:date
         :arc-authentication-results;
        bh=MziiEl/BGhmYyx6n5DVn+SwYFt2pUdO0M3B304cvZCc=;
        b=IsGBzLOFAqSNdDggrliu7PQ/gLU7lU9gsgAK6u9IFKxK0S+GngLnn7pGhvZcZ7vmMA
         R7SO0evAAK7Tw3T1yKfTwyc0DArVvghlKCXnpENOoiL3eoq6lriPInVC8WxMbYzH3Dnt
         iZkIs/cBjMobPv4TJhfUggld+mqBqUMMVhhEhYSEaTefV6HtyxKIk9b57qarj288hRJP
         t9QtohmBtsxsVrxofoVjkFsbtFD3phBOzPeJu8tMiqy6b3MycZLjhPSOVbWHUI+IAQBL
         8t2nfCgZEJbEkkDrWJqQGVjKJWBzWYfE5l3Wprp6ZiWXcQVuR3Ts6jRmzv6DObdpeU6S
         BpHA==
ARC-Authentication-Results: i=1; mx.google.com;
       spf=pass (google.com: domain of magfr@lysator.liu.se designates 130.236.254.3 as permitted sender) smtp.mailfrom=magfr@lysator.liu.se
Original-Received: from mail.lysator.liu.se (mail.lysator.liu.se. [130.236.254.3])
        by mx.google.com with ESMTPS id o7-v6si5945688lja.29.2018.05.21.00.07.51
        for <std-proposals@isocpp.org>
        (version=TLS1_2 cipher=ECDHE-RSA-CHACHA20-POLY1305 bits=256/256);
        Mon, 21 May 2018 00:07:51 -0700 (PDT)
Received-SPF: pass (google.com: domain of magfr@lysator.liu.se designates 130.236.254.3 as permitted sender) client-ip=130.236.254.3;
Original-Received: from mail.lysator.liu.se (localhost [127.0.0.1])
	by mail.lysator.liu.se (Postfix) with ESMTP id 914AF4002D;
	Mon, 21 May 2018 09:07:50 +0200 (CEST)
Original-Received: from noemi.bahnhof.se (h-155-4-132-137.NA.cust.bahnhof.se [155.4.132.137])
	(using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits))
	(No client certificate requested)
	by mail.lysator.liu.se (Postfix) with ESMTPSA id CEB6340008;
	Mon, 21 May 2018 09:07:49 +0200 (CEST)
Mail-Followup-To: std-proposals@isocpp.org,
	Raymund Hofmann <hofmannraymund@gmail.com>
Content-Disposition: inline
In-Reply-To: <cd751fc2-11ab-4e7c-b90e-5045f7df2c5c@isocpp.org>
X-Virus-Scanned: ClamAV using ClamSMTP
X-Original-Sender: magfr@lysator.liu.se
X-Original-Authentication-Results: mx.google.com;       spf=pass (google.com:
 domain of magfr@lysator.liu.se designates 130.236.254.3 as permitted sender) smtp.mailfrom=magfr@lysator.liu.se
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:38142
Archived-At: <http://permalink.gmane.org/gmane.comp.lang.c++.isocpp.proposals/38142>

On Sun, May 20, 2018 at 10:53:02PM -0700, Raymund Hofmann wrote:
> initializing members via the constructor repeats the type two times plus a 
> variable name three times:
> 
> struct S
> {
>     int i;
>     const char* cp;
>     S(int i, const char* cp) : i(i), cp(cp) {}
> };
> 
> I was trying to find a way improving on this.
> 
> The best i could come up with was something like:
> 
> struct T
> {
>     struct I
>     {
>         int i;
>         const char* cp;
>     } m;
>     T(const I& i) : m(i) {}
> };
> 
> usage:
> 
> void testf()
> {
>     S s(1, "abc");
>     T t({ 1,"abc" });
> }
> 
> Are there already some proposals/ideas for improving DRY on this?
> 
> My Idea:
> 
> I could think of a different class definition something like "lambda 
> capture style":
> 
> struct U (i=int(0); *cp="123") // <- "capture list"
> {
>   // members which are initialized to a fixed value don't go in the 
> "capture list", use regular members
>   // capture list generates a constructor to initialize the members implied 
> by the "capture list"
>   // usual class members/functions go here...
> };
> 
> usage:
> 
> void testf()
> {
>   U u(3,{});
> }
> 
> The generated constructor should also be available to delegate to, so 
> additional constructors can be had if desired.
> Maybe there is a better idea for integrating this "capture style" 
> initialization nicer and more compact when using it as a delegate.
> 

In C++-14 you can write

struct V
{
        int i = 0;
        const char* cp = "123";
};

V v1;
V v2 { 2, "234" };

Is that terse enough for you?

/MF

-- 
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/20180521070748.GA3811%40noemi.bahnhof.se.

.
