Skip to content

seeking approach for opencv code CV_NODISCARD and [[nodiscard]] #20544

@diablodale

Description

@diablodale

I'm seeking an approach for CV_NODISCARD that will work across all the compilers opencv 4.x and above supports.

The current approach is legacy for gcc and clang. Using __attribute__((__warn_unused_result__)) on functions. No classes. And always at the end of the function declaration. This is supported on gcc 4.8.1 and higher (since 4.8.1 is needed for C++11).

MSVC supports the standard attribute approach of [[nodiscard]]. It can be on classes, functions, vars. And it goes before the name of the item. At the end is not allowed. The support is available with VS2017 v15.3 and later; with /std:c++17 or higher.

So these are somewhat mutually exclusive. One or the other compiler (gcc vs msvc) will complain with warnings. It should still compile. Both compilers have a method to disable the warnings, but I read on the internet that on some versions of gcc, it ignores the ignore command and still puts the warning.

Does the core team have a recommendation?

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions