C++ Logo


Advanced search

Re: explicit class

From: Steve Weinrich <weinrich.steve_at_[hidden]>
Date: Mon, 11 Nov 2019 17:18:38 -0700
Hi Andrew,

Thank you for your questions.


I would definitely make any smart-pointer style class explicit. Also any class that manages another resource, like a file. The justification is to eliminate unexpected behavior prior to usage.


The primary design decision (IMHO) is how public the class is. For my own use, it likely doesn’t matter, but for a publicly visible class, it would certainly provide some safety.


I would probably start by marking all my classes as explicit and see how it goes. I certainly have been guilty of introducing unexpected behavior to myself!


I hope I have answered your questions.




From: Andrew Tomazos <andrewtomazos_at_[hidden]>
Sent: Monday, November 11, 2019 15:37
To: sotrdg sotrdg via Std-Proposals <std-proposals_at_[hidden]>
Cc: Steve Weinrich <weinrich.steve_at_[hidden]>
Subject: Re: [std-proposals] explicit class


What are some examples of classes that you would mark explicit? How would you make the design decision of whether to mark a class as explicit or not? Or would you recommend marking all of your classes explicit?


On Tue, Nov 12, 2019 at 5:47 AM Steve Weinrich via Std-Proposals <std-proposals_at_[hidden] <mailto:std-proposals_at_[hidden]> > 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?


Thanks in advance!

Std-Proposals mailing list
Std-Proposals_at_[hidden] <mailto:Std-Proposals_at_[hidden]> 

Received on 2019-11-11 18:20:59