lmi
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: [lmi] Rule of five exemplar


From: Vadim Zeitlin
Subject: Re: [lmi] Rule of five exemplar
Date: Thu, 14 Jul 2022 13:35:02 +0200

On Thu, 14 Jul 2022 00:20:13 +0000 Greg Chicares <gchicares@sbcglobal.net> 
wrote:

GC> On 7/13/22 21:29, Vadim Zeitlin wrote:
GC> > On Wed, 13 Jul 2022 20:51:24 +0000 Greg Chicares 
<gchicares@sbcglobal.net> wrote:
GC> > 
GC> > GC>  - add a very brief comment on any changed line
GC> > 
GC> >  I'm not sure that even this is needed -- if you delete the copy ctor, it
GC> > seems to say pretty clearly that you don't want the class to be copyable
GC> > and what else could the comment say?
GC> 
GC> > GC> +    // Rule of six: special member functions.
GC> > GC> +    ifstream()                           = default;
GC> > GC> +    ifstream(ifstream const&)            = delete;  // not yet needed
GC> > GC> +//  ifstream(ifstream&&)                 = default; // not yet needed
GC> > GC> +    ifstream& operator=(ifstream const&) = delete;  // std: only 
move=
GC> > GC> +//  ifstream& operator=(ifstream&&)      = default; // not yet needed
GC> 
GC> There are two possible reasons for not using "= default":
GC>  - I guess a defaulted member would work, but I don't want to spend time
GC>    thinking about it, and I don't need it right now anyway, so I'll just
GC>    inhibit it and not worry.
GC>  - I know that a defaulted member cannot work, and I know the reason why,
GC>    so let me document that before I forget it. Or, similarly, I tried
GC>    defaulting it, and clang warned that it would be defined as deleted,
GC>    so let me write '-Wdefaulted-function-deleted' for posterity.
GC> Isn't it worth writing a very few words to give the reason why, or to
GC> say there's no particular reason?

 I guess it could be if it's not obvious, but in many cases it is pretty
clear why it's deleted. I'm not against writing comments explaining
something non-trivial, of course, but I don't think mandating having a
comment is a good idea because quite often it won't add anything useful.

GC> Alternatively, if there's no particular reason, we might say nothing
GC> (there being nothing in particular to say); but if there's a reason,
GC> then say it. In the example above, that approach would have only one
GC> comment, the one on the copy assignment operator; I spent time trying
GC> to figure out why you had deleted it, and that led me to the tentative
GC> conclusion that it's because std::ifstream has only move assignment.

 Yes, std::ifstream is non-copyable (which seems relatively obvious because
files themselves can't be copied). I guess a comment saying "base class not
copyable" could be used for the deleted copy ctor and assignment operators,
but if we need such a comment wouldn't it be better to just say in the
class comment itself that "This class is not copyable because its base
class isn't either."?

VZ

Attachment: pgpUes7I1EHoc.pgp
Description: PGP signature


reply via email to

[Prev in Thread] Current Thread [Next in Thread]