C++ Logo

std-proposals

Advanced search

Re: = default { body... } on constructors and operator=

From: Tony V E <tvaneerd_at_[hidden]>
Date: Wed, 24 Feb 2021 11:19:32 -0500
Yep, I wrote myself a Tidy<int, -1> template just the other day.

But if we are all writing these workarounds, maybe we could either change the language instead, or standardize the workaround templates. 

One problem with the templates (for mine at least) is that they aren't robustly written. They do just-enough. Mine, for example, has ++ but not --.


Sent from my BlackBerry portable Babbage Device
From: Kilian Henneberger via Std-Proposals
Sent: Wednesday, February 24, 2021 11:07 AM
To: Bengt Gustafsson via Std-Proposals
Reply To: std-proposals_at_[hidden]
Cc: Kilian Henneberger
Subject: Re: [std-proposals] = default { body... } on constructors and operator=

Hi Bengt,

I suggest you wrap the m_fileHandle into a custom type which defines all the SMFs and ensures that each one does the right thing. And then you use that custom type as a member of RasterIO. You can just "=default;" the move-constructor of RasterIO and are done.

Regards,
Kilian

Am 24.02.2021 um 16:43 schrieb Bengt Gustafsson via Std-Proposals:

It seems rather common that move constructors and move assignment operators need to first move all members and then just set one member in the source to indicate that it has been moved from. Couldn't we allow a body after = default to handle this situation. Here is today's example:


RasterIO::RasterIO(RasterIO&& src) : m_fileName(std::move(src.m_fileName)), m_fileHandle(src.m_fileHandle), m_headerSize(src.m_headerSize),
    m_footerSize(src.m_footerSize), m_type(src.m_type), m_width(src.m_width), m_height(src.m_height), m_depth(src.m_depth),
    m_frames(src.m_frames), m_fileRowStride(src.m_fileRowStride),
    m_isRaw(src.m_isRaw), m_description(std::move(src.m_description))
{
    src.m_fileHandle = -1;
}


Which could be reduced to:


RasterIO::RasterIO(RasterIO&& src) = default {
    src.m_fileHandle = -1;
}

The current situation is both boring and reinstates the situation that = default was intended to remove, i.e. the verbosity and error proneness.

For constructors it would be possible to also allow : <initializers> between default and { which would allow special handling of a subset of the members, but I think this would rather rarely be useful.


What do you think?

Bengt



Received on 2021-02-24 10:19:35