Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[FEA]: Implement _CCCL_MAYBE_UNUSED? #2910

Open
1 task done
davebayer opened this issue Nov 20, 2024 · 2 comments
Open
1 task done

[FEA]: Implement _CCCL_MAYBE_UNUSED? #2910

davebayer opened this issue Nov 20, 2024 · 2 comments
Labels
feature request New feature or request.

Comments

@davebayer
Copy link
Contributor

davebayer commented Nov 20, 2024

Is this a duplicate?

Area

General CCCL

Is your feature request related to a problem? Please describe.

In many places in the code, there are various implementations of the [[maybe_unused]] behaviour which is unfortunately unavailable before C++17. My idea is to implement _CCCL_MAYBE_UNUSED macro that would be used in these cases instead.

Describe the solution you'd like

The macro could be implemented like this:

#if _CCCL_STD_VER >= 2017 && _CCCL_HAS_CPP_ATTRIBUTE(maybe_unused)
#  define _CCCL_MAYBE_UNUSED [[maybe_unused]]
#elif defined(_CCCL_COMPILER_MSVC)
  // MSVC has no equivalent to [[maybe_unused]], this is the best we can do
# define _CCCL_MAYBE_UNUSED _CCCL_DIAG_PUSH _CCCL_DIAG_SUPPRESS_MSVC(4100 4101 4189 4505) _CCCL_DIAG_POP
#else // ^^^ _CCCL_COMPILER_MSVC ^^^ / vvv !_CCCL_COMPILER_MSVC vvv
# define _CCCL_MAYBE_UNUSED __attribute__((__unused__))
#endif // ^^^ !_CCCL_COMPILER_MSVC ^^^

In C++17 we can just use the [[maybe_unused]] attribute. Otherwise with GCC, Clang, NVHPC and Intel compilers its easy, we can just use __attribute__((unused)). However, MSVC does not implement such behaviour. I tried to improvise with diagnostics - allowing and disabling the diagnostic warnings seems to work, with the fact that the disabled warnings are applied to the whole line instad of a single statement.

Here is a link with some test code: https://godbolt.org/z/jrnE8eq7e

I am mostly focusing on unused variables and functions, [[maybe_unused]] can be used in many other places.

Describe alternatives you've considered

No response

Additional context

No response

@davebayer davebayer added the feature request New feature or request. label Nov 20, 2024
@fbusato
Copy link
Contributor

fbusato commented Nov 20, 2024

+1. I recently needed this feature in CUB here

@bernhardmgruber
Copy link
Contributor

FYI: We are going to require C++17 in Thrust and CUB soon, so maybe don't spend too much time coming up with workarounds there.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
feature request New feature or request.
Projects
None yet
Development

No branches or pull requests

3 participants