C++ Logo

std-proposals

Advanced search

Re: [std-proposals] Attribute to require call to overridden base class function

From: Robin Savonen Söderholm <robinsavonensoderholm_at_[hidden]>
Date: Sun, 18 Aug 2024 10:10:06 +0200
To be honest, I say that this is one of the reasons for the design-rule
with private virtual functions. My suggested solution for this would be
something like:
```cpp
struct A {
private:
virtual void do_foo() = 0;
public:
void foo() {
  //do stuff
  do_foo();
  // maybe more stuff?
}
};

struct B : A {
private:
void do_foo() override { /*do stuff*/ }
};
```

I don't know if it happens often enough that this pattern does not
solve the problem without another attribute.

// Robin

On Sun, Aug 18, 2024, 09:42 Tiago Freire via Std-Proposals <
std-proposals_at_[hidden]> wrote:

> It's not about unit test, it's about communicating behavior.
>
> I can see it being a thing, but I'm not sure that really requiring it in
> all circumstances is a symptom of a design problem.
> Even the most pedantic of libraries I've worked with allows skipping the
> call to parent in some circumstances.
>
> I agree that it requires some user experience to get a better
> understanding of this.
>
>
> ------------------------------
> *From:* Std-Proposals <std-proposals-bounces_at_[hidden]> on behalf
> of Bo Persson via Std-Proposals <std-proposals_at_[hidden]>
> *Sent:* Sunday, August 18, 2024 9:30:18 AM
> *To:* std-proposals_at_[hidden] <std-proposals_at_[hidden]>
> *Cc:* Bo Persson <bo_at_[hidden]>
> *Subject:* Re: [std-proposals] Attribute to require call to overridden
> base class function
>
> On 2024-08-17 at 23:18, Billy via Std-Proposals wrote:
> > Could C++ use an attribute to require derived classes to call the base
> > class version of a function?
> >
> > struct A {
> > virtual void f() [[required]] { }
> > };
> >
> > struct B : A {
> > void f() override {
> > A::f(); // compiler warning if omitted
> > }
> > };
> >
> > Much like Objective-C's __attribute__((objc_requires_super))
> >
> >
>
> Does this have to be a compile time errror?
>
> In my experience, missing the call happens very rarely, and will be
> caught early in the unit tests.
>
>
> --
> Std-Proposals mailing list
> Std-Proposals_at_[hidden]
> https://lists.isocpp.org/mailman/listinfo.cgi/std-proposals
> --
> Std-Proposals mailing list
> Std-Proposals_at_[hidden]
> https://lists.isocpp.org/mailman/listinfo.cgi/std-proposals
>

Received on 2024-08-18 08:10:34