Skip to content

Use C23/C++17 attributes #159464

Open
Open

Description

@encukou

Currently, we use a bunch of compiler-specific attributes: for example Py_UNUSED is defined for GCC using __attribute__(( unused )) (in a #ifdef __GNUC__ block).

This makes for a bit of a problem for non-mainstream compilers: either they define __GNUC__ to pretend they're GCC, which means they need to implement "all" of GCC's quirks, or they don't, and they don't get nice features & optimizations.

C++17 & C23 have a solution to this: the [[attribute]] syntax. You can use [[maybe_unused]] (standard C) or [[gnu::unused]] (compiler-specific), after checking availability with #if defined(__has_c_attribute) && __has_c_attribute(maybe_unused).
Other compilers can then only provide the specific attribute to get the benefit. (Providing __has_c_attribute(gnu::unused) only means that this one feature behaves like in GCC.)

Like many new C/C++ features, existing compilers implement it in older-standard-mode as extensions. (And this is all about compiler extensions.)

We should start slowly migrating to this, see how it interacts with various pedantic modes we need to support. (Alas, this part is tricky.)
Presumably, any future attributes will be exposed this way; we should be ready to use them without compiler-specific #if ladders.

cc @vstinner @fuhsnn

Linked PRs

Activity

  1. self-assigned this
    on Oct 15, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Labels

Projects

No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions