C++ Logo

std-proposals

Advanced search

Re: [std-proposals] Detect non overriden function

From: Jason McKesson <jmckesson_at_[hidden]>
Date: Sat, 19 Mar 2022 17:05:01 -0400
On Sat, Mar 19, 2022 at 11:57 AM organicoman via Std-Proposals
<std-proposals_at_[hidden]> wrote:
>
> You haven't said why you'd like that and why a pure virtual won't help you. It
> would be a compilation error, not a warning, though.
>
> Thiago,
> Qt , has a class called QIODevice, this class is the base class of many derived classes.
> Some of the derived class, override a couple of methods, use the base class implementation for others and are indifferent to other base class methods.
> As an End user, and without checking the documentation, you use a method of one of these derived classes, but you are not sure if it overrides the base implementation or not.
> So obviously during debugging you don't suspect that the method you are using is not overrriden, and you keep looking somewhere else for the bug, which you will never know, especially with the jungle of signals and slots feature of Qt.
> So if i had a compiler warning telling me, "watchout, you are using the base class implementation", the first move is to check if the base class implementation covers my derived case.

To the extent that calling the base class implementation is *always*
wrong, that's the fault of the implementer of the class for not making
those methods pure-virtual. It's not something you can correct or
should be able to.

Have you considered that those methods are not pure-virtual for a
reason? If "using the base class implementation" is not *inherently*
an error, then a warning is not helpful.

> There is a hack to solve this, using the "curiously recurring template paradigm",
> But given the information is actually available at compile time, it is better to be a compiler feature.
>
> If you don't control the code, there's nothing to be done. C++
> generally doesn't allow code external to a class to modify the way you
> interface with that class, so I highly doubt the committee would
> introduce a feature to do that for something like this. You can create
> a new class that inherits from the base class, but that would require
> users to use your class instead of the base class.
>
> Jason,
> I would like to have this feature to communicate to my end users that some methods in the derived class he is about to use, are actually using the default implementation of the base class.
> Just as a warning.
> More information about the code is never bad.

If you have a class between the base class and the class the user
derives, then you can do whatever you like. If you want to force users
of your derived class to provide an implementation, then your
intermediate class can override the base class version as `final`, and
use pure-virtual to make them implement a different function that your
`final` function uses. For example:

```
class base_class
{
public:
  virtual void foo() {}
};

class intermediary : public base_class
{
public:
  void foo() final {my_foo();}

  virtual my_foo() = 0;
};

class derived : public intermediary
{
public:

};

derived d; //Compile error due to unimplemented pure-virtual function.
```

Received on 2022-03-19 21:05:25