220 17577 <CAFdMc-2rRmo4ZOZtKgdDu=ZBZQtLe9T693fvAUA_D4VF7n_MCA@mail.gmail.com> article
Path: news.gmane.org!not-for-mail
From: "dgutson ." <danielgutson@gmail.com>
Newsgroups: gmane.comp.lang.c++.isocpp.proposals
Subject: Re: explicit[ly] require initialization of attributes
Date: Wed, 29 Apr 2015 17:13:07 -0300
Lines: 176
Approved: news@gmane.org
Message-ID: <CAFdMc-2rRmo4ZOZtKgdDu=ZBZQtLe9T693fvAUA_D4VF7n_MCA@mail.gmail.com>
References: <CAFdMc-04EUwZZ39PMDZbGj73q4D_3OnFT7UpnJFC9sc+B9BgCA@mail.gmail.com>
	<CANh8DEmQEUM3BHLtWN1X94V+Ydvpm40bm=MHgE-kZ5tWEWh=CQ@mail.gmail.com>
Reply-To: std-proposals@isocpp.org
NNTP-Posting-Host: plane.gmane.org
Mime-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: quoted-printable
X-Trace: ger.gmane.org 1430338391 15192 80.91.229.3 (29 Apr 2015 20:13:11 GMT)
X-Complaints-To: usenet@ger.gmane.org
NNTP-Posting-Date: Wed, 29 Apr 2015 20:13:11 +0000 (UTC)
To: std-proposals <std-proposals@isocpp.org>
Original-X-From: std-proposals+bncBDE3NBMV6UFBBU7WQSVAKGQEK5VMD6A@isocpp.org Wed Apr 29 22:13:11 2015
Return-path: <std-proposals+bncBDE3NBMV6UFBBU7WQSVAKGQEK5VMD6A@isocpp.org>
Envelope-to: gclcip-std-proposals@m.gmane.org
Original-Received: from mail-ig0-f199.google.com ([209.85.213.199])
	by plane.gmane.org with esmtp (Exim 4.69)
	(envelope-from <std-proposals+bncBDE3NBMV6UFBBU7WQSVAKGQEK5VMD6A@isocpp.org>)
	id 1YnYM1-0005Zu-0Z
	for gclcip-std-proposals@m.gmane.org; Wed, 29 Apr 2015 22:13:09 +0200
Original-Received: by igchq3 with SMTP id hq3sf98130182igc.1
        for <gclcip-std-proposals@m.gmane.org>; Wed, 29 Apr 2015 13:13:08 -0700 (PDT)
X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed;
        d=1e100.net; s=20130820;
        h=x-gm-message-state:mime-version:in-reply-to:references:date
         :message-id:subject:from:to:content-type:content-transfer-encoding
         :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=CP+fAVaD9iA5zHSTac24+Pfc+oPVrU69u1dZw+jMaC8=;
        b=RR/VoSW3x7SSViVorq2fIZcANGlshCWsDAsBdNFR8D+DQAdd9Za0MQyLss5MIdwEHz
         YzHRy0mp7ZLKutPmgdyRDSfl2j0XUCKxI0ZGneVkcW9AupxGOVLzvLUn3Uv3ar096l4t
         MmRcGPVaczy0MQ70t7x/JC3AsLyxuj8noeEABugl18mOss/Nd8fcoNFlct8VM8/Fkp8/
         snu7YWAf5HXqnB5awrPtSxSxNq07jVKf/iab3rmyCWo+5j6AxNyZWCY4c/X+lpNOF4Ce
         uvsMsWPNPUIlAiY3/UGZ6sSOtyy1IwvJWMLGiFVVAYZNISrRWL40WoXWjnTzN5PnYed9
         EjZQ==
X-Gm-Message-State: ALoCoQm+nWi2AjfkA/99J+I16WGoretoO/wGX1KhSlkAlKY2lQqO/6wpM2JfL68JI+Sf57hBWYjR
X-Received: by 10.50.134.202 with SMTP id pm10mr16640067igb.2.1430338388037;
        Wed, 29 Apr 2015 13:13:08 -0700 (PDT)
X-BeenThere: std-proposals@isocpp.org
Original-Received: by 10.50.138.138 with SMTP id qq10ls2217127igb.35.canary; Wed, 29
 Apr 2015 13:13:07 -0700 (PDT)
X-Received: by 10.107.134.206 with SMTP id q75mr1092596ioi.27.1430338387573;
        Wed, 29 Apr 2015 13:13:07 -0700 (PDT)
Original-Received: from mail-ig0-x231.google.com (mail-ig0-x231.google.com. [2607:f8b0:4001:c05::231])
        by mx.google.com with ESMTPS id gy8si391903icb.23.2015.04.29.13.13.07
        for <std-proposals@isocpp.org>
        (version=TLSv1.2 cipher=ECDHE-RSA-AES128-GCM-SHA256 bits=128/128);
        Wed, 29 Apr 2015 13:13:07 -0700 (PDT)
Received-SPF: pass (google.com: domain of danielgutson@gmail.com designates 2607:f8b0:4001:c05::231 as permitted sender) client-ip=2607:f8b0:4001:c05::231;
Original-Received: by igblo3 with SMTP id lo3so127792391igb.1
        for <std-proposals@isocpp.org>; Wed, 29 Apr 2015 13:13:07 -0700 (PDT)
X-Received: by 10.50.93.69 with SMTP id cs5mr2692646igb.4.1430338387364; Wed,
 29 Apr 2015 13:13:07 -0700 (PDT)
Original-Received: by 10.36.55.5 with HTTP; Wed, 29 Apr 2015 13:13:07 -0700 (PDT)
In-Reply-To: <CANh8DEmQEUM3BHLtWN1X94V+Ydvpm40bm=MHgE-kZ5tWEWh=CQ@mail.gmail.com>
X-Original-Sender: danielgutson@gmail.com
X-Original-Authentication-Results: mx.google.com;       spf=pass (google.com:
 domain of danielgutson@gmail.com designates 2607:f8b0:4001:c05::231 as
 permitted sender) smtp.mail=danielgutson@gmail.com;       dkim=pass
 header.i=@gmail.com;       dmarc=pass (p=NONE dis=NONE) header.from=gmail.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: <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:17577
Archived-At: <http://permalink.gmane.org/gmane.comp.lang.c++.isocpp.proposals/17577>

On Wed, Apr 29, 2015 at 5:09 PM, 'Matt Calabrese' via ISO C++ Standard
- Future Proposals <std-proposals@isocpp.org> wrote:
> Can't this be done with C++11 attributes? It could just be a datamember

I don't back the attributes-solution because those are ignorable, and
I'm looking for a tool for the programmer to make a program
ill-formed if the data member wasn't initialized (or in Ville's,
example, the function argument comes from a conversion).

> attribute that requires that member to be explicitly constructed. It woul=
d
> be something like [[explicit_construction]] and would suggest that the
> compiler emits a diagnostic if the member is not explicitly initialized.
>
> I'm not a fan of the template approach because it changes the type of the
> datamember and requires indirect access through the new datamember type
> (i.e. via a get() accessor), all simply because we want the member to be

Well, a getter wouldn't be necessary in a good library design.

> explicitly initialized. If this is really a desired feature, I think it
> makes more sense as an in-language specifier/attribute.
>
> On Wed, Apr 29, 2015 at 11:57 AM, dgutson . <danielgutson@gmail.com> wrot=
e:
>>
>> I think I've seen this.
>>
>> Problem:
>>   - when adding a new ctor, a programmer may inadvertently leave
>> uninitialized attributes (specially PODs). Similarly, when adding a
>> new attribute, a programmer may forget to call its ctor (or initialize
>> it) in all the ctors of the class (again, specially when the new
>> attribute is a POD).
>>
>> Problem Example1:
>>
>> //before the change:
>> struct S
>> {
>>     int x;
>>     S(/*some signature*/) : x(1) {}
>> };
>>
>> //after the change:
>> struct S
>> {
>>     int x;
>>     S(/*some signature*/) : x(1) {}
>>     S(/*some OTHER signature*/) {} // x is uninitialized
>> };
>>
>> Problem Example 2:
>> //before the change:
>> struct S
>> {
>>     int x;
>>     S(/*some signature*/) : x(1) {}
>> };
>>
>> //after the change:
>> struct S
>> {
>>     int x;
>>     int y; // new attribute
>>     S(/*some signature*/) : x(1) {} // y is uninitialized
>> };
>>
>> - Why this problem is important?
>>     The more attributes / ctors the class has, the problem gets worse
>> (though that might be caused by a bad design of poor modularization).
>>     The problem is relevant when writing maintainable code, since
>> future maintainers may incur in the errors mentioned above.
>>
>> - How people is working around this issue nowadays?
>>     There are both static checkers and dynamic checkers (such as
>> valgrind) that may detect use of uninitialized data.
>>     People can =3Ddelete the default ctor of the contained objects.
>>
>> - Alternative proposed solutions
>>    Solution 1: library approach. Provide a type wrapper without a
>> default ctor (such as initialization_required<T>). Such wrapper could
>> be implemented extending the template type (inheriting from it) and
>> removing the default ctor, and should be template-specialized for PODs
>> with all the operations of each POD.
>>
>> struct S
>> {
>>     initialization_required<int> x;
>>     initialization_required<int> y;
>>    S(/*some signature*/) : x(1) {} // error: y initialization missing
>> };
>>
>>    Solution 2: prefix the attribute declaration with some keyword
>> (such as "explicit").
>>
>> struct S
>> {
>>     explicit int x;
>>     explicit int y;
>>     S(/*some signature*/) : x(1) {} // error: y is uninitialized
>> };
>>
>>  - Discussion: what happens with inheritance?
>>      As mentioned above, this problem gets relevant with PODs so
>> inheritance wouldn't apply in that case (unless we propose the ability
>> to extend from PODs some day ;-) ).
>>      Nevertheless, both solutions could be applicable to inheritance:
>>
>> Sol1:   struct S : initialization_required<T> { ... };
>> Sol2:   struct S : explicit T { ... };
>>
>> Ideas? Is this worth to write a formal proposal?
>>
>> Thanks,
>>
>>     Daniel.
>>
>>
>>
>> --
>> Who=E2=80=99s got the sweetest disposition?
>> One guess, that=E2=80=99s who?
>> Who=E2=80=99d never, ever start an argument?
>> Who never shows a bit of temperament?
>> Who's never wrong but always right?
>> Who'd never dream of starting a fight?
>> Who get stuck with all the bad luck?
>>
>> --
>>
>> ---
>> You received this message because you are subscribed to the Google Group=
s
>> "ISO C++ Standard - Future Proposals" group.
>> To unsubscribe from this group and stop receiving emails from it, send a=
n
>> 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/.
>
>
> --
>
> ---
> 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/.



--=20
Who=E2=80=99s got the sweetest disposition?
One guess, that=E2=80=99s who?
Who=E2=80=99d never, ever start an argument?
Who never shows a bit of temperament?
Who's never wrong but always right?
Who'd never dream of starting a fight?
Who get stuck with all the bad luck?

--=20

---=20
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 e=
mail 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-proposa=
ls/.

.
