C++ Logo

std-proposals

Advanced search

Re: explicit class

From: Steve Weinrich <weinrich.steve_at_[hidden]>
Date: Mon, 11 Nov 2019 17:26:17 -0700
Hello Andrey,

The practical case is primarily for the developer of a class, not the user of it. Think about a smart-pointer class. During development, one is required to consider every possible aspect of the class. Using explicit, one could develop only the minimum aspects and add others as required and/or realized. I have certainly been surprised myself on many occasions by using operators that I had not considered.

As to the destructor, my answer is yes. Consider a class that holds global state for a non-terminating process. We would certainly want the developer to consider what should happen if the object somehow gets destroyed! The explicit keyword would force such thinking.

Thank you for your questions and regards.

-----Original Message-----
From: Std-Proposals <std-proposals-bounces_at_[hidden]> On Behalf Of Andrey Semashev via Std-Proposals
Sent: Monday, November 11, 2019 15:46
To: std-proposals_at_[hidden]
Cc: Andrey Semashev <andrey.semashev_at_[hidden]>
Subject: Re: [std-proposals] explicit class

On 2019-11-11 21:47, Steve Weinrich via Std-Proposals wrote:
> This is an idea that has been roaming around my head for some 20 years!
>
> As we know, there is a list of some 15 (or so) operations
> (construction, assignment, copy, move, conversion, etc.) that the simple statement:
> “class T {};” defines.
>
> If one wishes some control over this behavior, one has to be
> knowledgeable in all that the compiler provides. In non-C++ terms,
> one has to say (modern use: =delete), “I don’t want that, I don’t want
> this, etc.” The compiler provides no aid for this!
>
> My notion is to reverse this via “explicit class T {};”. While I am
> sure there are some details that would need to be debated, this would
> eliminate (most) everything that the compiler defines for you. Thus,
> if you wanted something, you would have to define it or use “ = default”
> (yes, I am aware that would require extending “= default” to more
> items – seems OK to me). The obvious advantage is that the compiler
> now provides some help, in that using something that is not defined
> will yield a compiler error.
>
> Does anyone know if this has been proposed before? If not, what do
> you think?

I don't see a practical reason why someone would want to mark a class explicit. Sure, there are cases when one wants to remove some operations on the class, but I fail to see a case when I would want to remove everything.

Also, does your proposal remove the destructor as well?
--
Std-Proposals mailing list
Std-Proposals_at_[hidden]
https://lists.isocpp.org/mailman/listinfo.cgi/std-proposals

Received on 2019-11-11 18:28:37