C++ Logo

std-proposals

Advanced search

Re: [std-proposals] explicit this

From: Jason McKesson <jmckesson_at_[hidden]>
Date: Tue, 4 Apr 2023 11:15:15 -0400
On Mon, Apr 3, 2023 at 4:32 AM Frederick Virchanza Gotham via
Std-Proposals <std-proposals_at_[hidden]> wrote:
>
> Sometimes when I'm reading through another person's implementation of
> a class, I see function calls inside member functions:
>
> void SomeClass::SomeFunc(void)
> {
> SomeOtherFunc();
> }
>
> When I first see this code, I don't know if SomeOtherFunc is a member
> function, or whether it's a member datum (i.e. a function pointer), or
> whether it's a global function.
>
> When I write the implementation of my own classes, I tend to like to
> be specific:
>
> void SomeClass::SomeFunc(void)
> {
> this->SomeOtherFunc();
> }
>
> I was thinking it would be good if we could mark a class, or perhaps
> mark a member function, as "explicit this", as follows:
>
> class SomeClass : explicit this {
>
> void (*SomeOtherFunc)(void);
>
> void SomeFunc(void)
> {
> SomeOtherFunc(); /* compiler error */
> }
> };
> --
> Std-Proposals mailing list
> Std-Proposals_at_[hidden]
> https://lists.isocpp.org/mailman/listinfo.cgi/std-proposals

I really do not feel that the language should be in the business of
empowering users to police programming style questions. The things you
want to prevent are legal C++ code, and generally speaking, preventing
legal C++ code from working is not something a language should do.

Indeed, most attributes don't do this. The closest one that does is
[[nodiscard]], whose job is to prevent a user from ignoring what the
function/class is *supposed to do*. That is, if you do discard it
legally, you have almost certainly written your code wrong.

That's just not the case here. Not explicitly using `this->` is
perfectly reasonable. What you're asking for is purely a stylistic
convention. And that's not what language features should deal with.

Received on 2023-04-04 15:15:28