lmi
[Top][All Lists]
Advanced

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

Re: [lmi] depr.impldec


From: Vadim Zeitlin
Subject: Re: [lmi] depr.impldec
Date: Mon, 11 Jul 2022 23:55:21 +0200

On Mon, 11 Jul 2022 19:28:50 +0000 Greg Chicares <gchicares@sbcglobal.net> 
wrote:

GC> On 7/10/22 12:29, Vadim Zeitlin wrote:
GC> [...]
GC> >  FWIW here is my personal strategy for defining the class' special
GC> > functions: there are several categories of classes
GC> > 
GC> > 1. Value-like: for them copy special functions and dtor are explicitly
GC> >    defaulted. Move special functions are omitted, and are not available.
GC> 
GC> Already I'm struggling here, but I think the issue is nomenclature.
GC> 
GC> Is this...
GC> 
GC> class custom_string
GC> {
GC>   public:
GC>     custom_string(std::string const& arg) : s_(arg) {}
GC>     custom_string() = default;
GC> 
GC>     custom_string& operator=(std::string const& arg) {s_ = arg; return 
*this;}
GC>     std::string const& value() const {return s_;}
GC> 
GC>   private:
GC>     std::string s_;
GC> };
GC> 
GC> a "value-like" class?

 Yes, it is, but it's also "container-like", where for me the difference is
that non-container values are cheap to copy while containers are not
necessarily. So for cheap to copy values move operations don't really
provide any gain, and can be omitted -- but can also be defaulted, as here.

GC> For this class, would you follow guideline (1) above, making
GC> the move special functions unavailable?

 No, I'd default them. Although if such strings are usually short this
wouldn't make much, or even any, difference, because moving a string using
short string optimization is not faster than copying it (might be slower,
actually, although I doubt the difference is going to be noticeable).

GC> I think it already does the right thing by declaring no
GC> special member function except a default ctor. The default
GC> implementations of the other five special operations can't
GC> be wrong, because std::string is movable and copyable.
GC> And the move operations may be useful.

 Yes, I agree, my rules in the previous message were over-simplified. Move
operations should be enabled/defaulted for the value-like classes if
copying them can be expensive and, in the absence of any other information,
this certainly could be the case for custom_string here.

 Sorry for being sloppy in my original formulation,
VZ

Attachment: pgpPw5QDtuKUX.pgp
Description: PGP signature


reply via email to

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