C++ Logo

std-proposals

Advanced search

Re: [std-proposals] Detect non overriden function

From: organicoman <organicoman_at_[hidden]>
Date: Sun, 20 Mar 2022 06:36:55 +0400
```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.```That's if your intention is to make 'intermediary' a non final product i.e unusable by end user without deriving, and obliging him to derive + implement before usage.Me , i want the code to run without imposing anything upon the end user, but nevertheless i want him to be aware of what's going on.Take the following case scenario:Imagine the virtual method in your 'base_class', is general enough and covers most needs of derived classes for that function. But in one or two derived classes you want to give the end user more introspection, and control over the implementation of that exact method. So usually, you derive from the base class, don't override the method in question to keep the general usage, but you provide two other member functions which will give you more fine tuned control of the implementation. So from the perspective of the end user, he sees that for more control, use the other member functions, and for the usual general usage the base method is the fall back, but he would never guess that the base method was not overrriden in the class he is using, and probably won't give him the results he expects, and without going back to the documentation he will never guess that shortage.So if the compiler gives him a heads-up "hey you are directly using the base class method, not the overrriden one", he will automatically investigate if the base class implementation of that method will cover his intended usage.And because there are so many base methods not overriden in the derived class, to avoid pages of warnings, you just decorate the interesting ones only for example://///class base{ virtual void foo() {}};class derived: public base{ [non_overriden] base::foo; //will warn when called };//////This could be a plausible way of implementing this feature.I could imagine that this feature will free the developer from over using the abstract class design.NadSent from my Galaxy
-------- Original message --------From: Jason McKesson via Std-Proposals <std-proposals_at_[hidden]> Date: 3/20/22 1:05 AM (GMT+04:00) To: std-proposals_at_[hidden] Cc: Jason McKesson <jmckesson_at_[hidden]> Subject: Re: [std-proposals] Detect non overriden function 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 makingthose methods pure-virtual. It's not something you can correct orshould be able to.Have you considered that those methods are not pure-virtual for areason? 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 userderives, then you can do whatever you like. If you want to force usersof your derived class to provide an implementation, then yourintermediate class can override the base class version as `final`, anduse 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.```-- Std-Proposals mailing listStd-Proposals_at_[hidden]://lists.isocpp.org/mailman/listinfo.cgi/std-proposals

Received on 2022-03-20 02:37:05