C++ Logo

std-proposals

Advanced search

Re: [std-proposals] explicit class (2023, 2019, 2004, 2002)

From: Frederick Virchanza Gotham <cauldwell.thomas_at_[hidden]>
Date: Tue, 11 Jun 2024 15:21:02 +0100
On Tue, Jun 11, 2024 at 2:18 PM Jonathan Wakely wrote:
>
> What's a miranda function?


I learned C++ from the book "C++ For Dummies" back in 2002. The author
Stephen R. Davis wrote:

    "I like to call the Miranda functions, ie. if you can't supply
your own, the court will supply them for you"

Miranda functions are, for example, copy constructor, move
constructor, assignment operator and so on. Anything that's given to
you without you needing to ask for it.


> Why can't explicit(false) be used here?


That syntax could work too.


> Why is making constructors explicit by default related to
> making special member functions omitted by default?
> Those seem like two unrelated features.


The idea of marking a class as 'explicit' is just to make it very
restrictive in many ways. Some people like to be very concise when
writing code. I like to be pedantic and verbose, as in: "I want the
compiler to ask me to clarify what I meant to do unless I've made it
crystal clear what I meant to do".

There is a majority of neurotypical people and a minority of
neurodiverse people in society. Some estimate that the ratio is 99:1.
When it comes to computer programmers, that minority is a lot bigger,
with a ratio of something like 13:1. And as you go up in skill level
with computer programmers, that ratio gets closer and closer. If we
were to survey all of the people who compose papers to present to the
C++ committee, I'd believe you if you told me half of them were
neurodiverse.

The reason I'm getting into all this is that I think a lot of
programmers want the compiler to demand more clarification from the
programmer. That's why I dug up posts from 3 other people over the
past 22 years to show that other people want this too.


>> 3 - A derived class's member function which overrides a base
>> class's virtual function must be marked as both 'virtual' and
>> 'override'.
>
>
> Why? That goes against all guidelines I'm aware of, which say to omit
> the redundant 'virtual' when using 'override'.


I like to have "virtual" on the far left because when you're scrolling
down through the header file, you see the "virtual" first (as we
normally read from left to right). Maybe programmers who started out
with Hebrew or Arabic will have a stronger tendency to look to the
right (but even readers of Hebrew and Arabic will be used to the text
being aligned on the right, so when it's unaligned on the right then
maybe they're more likely to look to the left). I want to scan my eye
down the left side of the screen and immediately see which methods are
virtual.


> wat
>
> This seems a lot less bizarre:
> virtual func() override(false)


When I see your line of code, I think:

    "This is a virtual member function, and there is no such virtual
function in any of the base classes"

But I wanted to convey:

    "This is a virtual member function, and there is a non-virtual
method with the same name in at least one of the base classes"


> Why? What has this got to do with the other features?
>
> Why should writing 'explicit' before the class-head enable a bunch of unrelated features?
> Is there any motivation for this?


For example when you see a function like:

      void SomeClass::SomeFunc(void)
      {
          SomeOtherFunc();
      }

You don't know if "SomeOtherFunc" is member function. I prefer to write:

      void SomeClass::SomeFunc(void)
      {
          this->SomeOtherFunc();
      }


>> Note that the constructor's initialiser list has "this->n(7)" instead
>> of "n(7)", and this is so that you can have a member with the same
>> name as a base class, as follows:
>
>
> Why is that useful?
> Why can't you solve it with a private typedef for the base class?


Some would argue that the 'typedef' is a workaround for a lack of
restrictiveness.

Received on 2024-06-11 14:21:15