Skip to content

add nodiscard to features2d clone funcs (2nd review for nodiscard)#20675

Merged
opencv-pushbot merged 1 commit intoopencv:3.4from
diablodale:fix2-20544
Sep 9, 2021
Merged

add nodiscard to features2d clone funcs (2nd review for nodiscard)#20675
opencv-pushbot merged 1 commit intoopencv:3.4from
diablodale:fix2-20544

Conversation

@diablodale
Copy link
Copy Markdown
Contributor

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_STD was 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

  • I agree to contribute to the project under Apache 2 License.
  • To the best of my knowledge, the proposed patch is not based on a code under GPL or other license that is incompatible with OpenCV
  • The PR is proposed to proper branch
  • There is reference to original bug report and related work
  • The feature is well documented and sample code can be built with the project CMake

@alalek
Copy link
Copy Markdown
Member

alalek commented Sep 8, 2021

CV_NODISCARD_STD was not put on the related pure virtual

Why? Users may use base class and its methods in a wrong way too.

@diablodale
Copy link
Copy Markdown
Contributor Author

CV_NODISCARD_STD was not put on the related pure virtual

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.

@diablodale
Copy link
Copy Markdown
Contributor Author

diablodale commented Sep 8, 2021

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 myclass::mypurevirtual(x, y, z) and it work. The compiler will stop compiling with a critical error something like "no implementation".

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.

@alalek
Copy link
Copy Markdown
Member

alalek commented Sep 8, 2021

a pure virtual defines an interface and nothing else

Users can call this interface and they should be able get warnings.
Moreover API may not have derived classes on pubic level: just base interface + factory.

Example (Clang is smart, GCC doesn't).

@diablodale
Copy link
Copy Markdown
Contributor Author

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.
If I change these to not be virtual, then GCC 7.1 correctly reports discarding. But virtual...it does not report.
gcc knows about the bug https://gcc.gnu.org/bugzilla/show_bug.cgi?id=84476

MSVC works correctly.

I'll make the change and test it Thursday. I want to be sure that the python CV_WRAP doesn't have problems as the declaration would look like

CV_WRAP CV_NODISCARD_STD virtual Ptr<DescriptorMatcher> clone( bool emptyTrainData=false ) const = 0;

@alalek
Copy link
Copy Markdown
Member

alalek commented Sep 8, 2021

I want to be sure that the python CV_WRAP doesn't have problems

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

decl_str = self.batch_replace(decl_str,
    [
        ("static inline", ""),
        ("inline", ""),
        ("explicit ", ""),
        ("CV_EXPORTS_W", ""),
        ("CV_EXPORTS", ""),
        ("CV_CDECL", ""),
        ("CV_WRAP ", " "),
        ("CV_INLINE", ""),
        ("CV_DEPRECATED", ""),
        ("CV_DEPRECATED_EXTERNAL", ""),
        ("CV_NODISCARD_STD", ""),
        ("CV_NODISCARD", ""),
    ]).strip()

@diablodale
Copy link
Copy Markdown
Contributor Author

force pushed, includes nodiscard on pure virtual + add nodiscard handling to wrapper parser
I followed python formatting style of similar list in the same file (line 603)
python module tests passed on my local setup

Copy link
Copy Markdown
Member

@alalek alalek left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks good to me 👍

@opencv-pushbot opencv-pushbot merged commit ac0fd6a into opencv:3.4 Sep 9, 2021
@alalek alalek mentioned this pull request Sep 11, 2021
@alalek alalek mentioned this pull request Oct 15, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants