Skip to content

[C API] Py_ARRAY_LENGTH() macro is incompatible with g++ -std=gnu++11 #158810

Description

@vstinner

Bug report

Example:

#include <Python.h>

int main()
{
    int array[] = {1, 2, 3};
    return Py_ARRAY_LENGTH(array);
}

Building this example with g++ -std=gnu++11 example.cpp or just g++ example.cpp fails with:

In file included from Include/Python.h:70,
                 from x.cpp:1:
x.cpp: In function ‘int main()’:
Include/pymacro.h:217:30: error: ‘__builtin_types_compatible_p’ was not declared in this scope
  217 |      + Py_BUILD_ASSERT_EXPR(!__builtin_types_compatible_p(typeof(array), \
      |                              ^~~~~~~~~~~~~~~~~~~~~~~~~~~~

The example builds correctly if I use -std=c++11 (g++ -std=c++11 example.cpp).

The macro is currently defined as:

#define Py_ARRAY_LENGTH(array) \
    (sizeof(array) / sizeof((array)[0]) \
     + Py_BUILD_ASSERT_EXPR(!__builtin_types_compatible_p(typeof(array), \
                                                          typeof(&(array)[0]))))
#else
#define Py_ARRAY_LENGTH(array) \
    (sizeof(array) / sizeof((array)[0]))
#endif

I'm not sure why __builtin_types_compatible_p() is available with -std=c++11, but not with -std=gnu++11.

I made my tests using g++ (GCC) 16.2.1 on Fedora 44.

CPython versions tested on:

CPython main branch

Operating systems tested on:

Linux

Linked PRs

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    buildThe build process and cross-buildinterpreter-core(Objects, Python, Grammar, and Parser dirs)topic-C-APItype-bugAn unexpected behavior, bug, or error

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions