add nodiscard to features2d clone funcs (2nd review for nodiscard)#20675
add nodiscard to features2d clone funcs (2nd review for nodiscard)#20675opencv-pushbot merged 1 commit intoopencv:3.4from diablodale:fix2-20544
Conversation
Why? Users may use base class and its methods in a wrong way too. |
Due to official c++ spec. A [[nodiscard]] attribute on a virtual is not inherited, therefore it is useless and unneeded on the pure virtual. It is only put on the two implementations. There is no base class or implementation on a pure virtual. Therefore it is impossible to discard something which will never exist...because a pure virtual never exists. |
|
A better way to write that is...a pure virtual never has an instance and never has an implementation. There is no definition...no code. It is IMpossible to write a pure virtual defines an interface and nothing else. It is some derived class that implements it. And it is on that derived class that there can be code to execute...or have a class instance and can therefore have a nodiscard. |
Users can call this interface and they should be able get warnings. Example (Clang is smart, GCC doesn't). |
|
Ah yes. Thanks for the use case. You're right, I'll add it. I didn't consider not creating an instance...instead passing around the pointers with vtables. I explored and isolated that GCC fails to correctly use [[nodiscard]] on virtual functions. MSVC works correctly. I'll make the change and test it Thursday. I want to be sure that the python CV_WRAP CV_NODISCARD_STD virtual Ptr<DescriptorMatcher> clone( bool emptyTrainData=false ) const = 0; |
Perhaps we need to update this list (perhaps we need to have 1 item per line): https://github.com/opencv/opencv/blob/4.5.3/modules/python/src2/hdr_parser.py#L437-L443 |
|
force pushed, includes nodiscard on pure virtual + add nodiscard handling to wrapper parser |
2nd pass of fixes #20544
Originally intended to include any needed changes for 4.x branch. During which, found these opportunities.
Should work on 3.x and 4.x
CV_NODISCARD_STDwas not put on the related pure virtual. By official c++ spec, a [[nodiscard]] attribute on a virtual is not inherited, therefore it is useless and unneeded on the pure virtual. It is only put on the two implementations.Pull Request Readiness Checklist
See details at https://github.com/opencv/opencv/wiki/How_to_contribute#making-a-good-pull-request