https://gcc.gnu.org/bugzilla/show_bug.cgi?id=81513

Jonathan Wakely <redi at gcc dot gnu.org> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
                 CC|                            |jason at gcc dot gnu.org

--- Comment #3 from Jonathan Wakely <redi at gcc dot gnu.org> ---
(In reply to Pavel Roskin from comment #2)
> __has_cpp_attribute is not supposed to check if the functionality is
> available somehow using some other approaches and keywords. It is supposed
> to check if the functionality is available as an attribute.

__has_cpp_attribute(X) tells you if X is a valid attribute. To know if you can
also use the C++11 attribute syntax you can use __cpp_attributes >= 200809.

So I would write your test as:

#if __cpp_attributes >= 200809 && defined(__has_cpp_attribute) \
  && __has_cpp_attribute(maybe_unused)
# define __maybe_unused [[maybe_unused]]
#endif

#ifndef __maybe_unused
# define __maybe_unused
#endif

That will make it work in C++03 mode.

If your bug report is that __has_cpp_attribute doesn't tell you if C++11
attribute syntax is supported, then the bug report is invalid. You should be
using __cplusplus >= 201103L for that (or the non-standard but WG21-approved
__cpp_attributes >= 200809), not assuming that the existence of
__cpp_has_attribute implies C++11 attributes work.

The reason is that you can use __has_cpp_attribute(gnu::unused) in C++03 mode
to test if __attribute__((unused)) will work.

Maybe we should make __has_cpp_attribute false in C++03 mode for any attribute
without the gnu:: prefix, if such attributes really aren't usable without C++11
syntax.

> Even is there is some alternative notation for C++03, I expect the attribute
> name to be the same. If __has_cpp_attribute(maybe_unused) is set to a
> non-zero value, I expect to use "maybe_unused", not "unused".

I'm not disagreeing, I was only observing that the other attribute name does
work as __attribute__((maybe_unused)). But I think that's because the correct
test for that would be __has_cpp_attribute(gnu::maybe_unused) (which is false,
as expected).


We should probably document the behaviour of __has_cpp_attribute and how it
interacts with GNU-style attributes as well as C++11-style attributes.

Reply via email to