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

            Bug ID: 108965
           Summary: g++: unable to parse c11 _Generics
           Product: gcc
           Version: 11.3.0
            Status: UNCONFIRMED
          Severity: normal
          Priority: P3
         Component: c++
          Assignee: unassigned at gcc dot gnu.org
          Reporter: chrisfriedt at gmail dot com
  Target Milestone: ---

G++ version 11.3.0 fails to parse the C11 _Generic keyword. I would suspect
that earlier versions also fail to do this and likely also that later versions
fail as well.

gcc --version
gcc (Ubuntu 11.3.0-1ubuntu1~22.04) 11.3.0
Copyright (C) 2021 Free Software Foundation, Inc.
This is free software; see the source for copying conditions.  There is NO
warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.

It's not clear to me if any part of the ISO C++ standard requires a C++
compiler to parse C11 _Generic, but it certainly can make life less pleasant,
as the equivalent C++ code can sometimes rely on static inline constexpr
templates which are apparently "not cool" until C++14. This particularly
affects people who are using somewhat older C++ toolchains that do not yet
support C++14.

LLVM has no problem, OTOH.

Sample taken from https://en.cppreference.com/w/c/language/generic
```
#include <stdio.h>
#include <math.h>

// Possible implementation of the tgmath.h macro cbrt
#define cbrt(X) _Generic((X), \
              long double: cbrtl, \
                  default: cbrt,  \
                    float: cbrtf  \
              )(X)

int main(void)
{
    double x = 8.0;
    const float y = 3.375;
    printf("cbrt(8.0) = %f\n", cbrt(x)); // selects the default cbrt
    printf("cbrtf(3.375) = %f\n", cbrt(y)); // converts const float to float,
                                            // then selects cbrtf
}
```

```
g++ -o /tmp/main /tmp/main.cpp
/tmp/main.cpp: In function ‘int main()’:
/tmp/main.cpp:6:15: error: expected primary-expression before ‘long’
    6 |               long double: cbrtl, \
      |               ^~~~
/tmp/main.cpp:15:32: note: in expansion of macro ‘cbrt’
   15 |     printf("cbrt(8.0) = %f\n", cbrt(x)); // selects the default cbrt
      |                                ^~~~
/tmp/main.cpp:7:19: error: expected primary-expression before ‘default’
    7 |                   default: cbrt,  \
      |                   ^~~~~~~
/tmp/main.cpp:15:32: note: in expansion of macro ‘cbrt’
   15 |     printf("cbrt(8.0) = %f\n", cbrt(x)); // selects the default cbrt
      |                                ^~~~
/tmp/main.cpp:8:21: error: expected primary-expression before ‘float’
    8 |                     float: cbrtf  \
      |                     ^~~~~
/tmp/main.cpp:15:32: note: in expansion of macro ‘cbrt’
   15 |     printf("cbrt(8.0) = %f\n", cbrt(x)); // selects the default cbrt
      |                                ^~~~
/tmp/main.cpp:5:17: error: ‘_Generic’ was not declared in this scope
    5 | #define cbrt(X) _Generic((X), \
      |                 ^~~~~~~~
/tmp/main.cpp:15:32: note: in expansion of macro ‘cbrt’
   15 |     printf("cbrt(8.0) = %f\n", cbrt(x)); // selects the default cbrt
      |                                ^~~~
/tmp/main.cpp:6:15: error: expected primary-expression before ‘long’
    6 |               long double: cbrtl, \
      |               ^~~~
/tmp/main.cpp:16:35: note: in expansion of macro ‘cbrt’
   16 |     printf("cbrtf(3.375) = %f\n", cbrt(y)); // converts const float to
float,
      |                                   ^~~~
/tmp/main.cpp:7:19: error: expected primary-expression before ‘default’
    7 |                   default: cbrt,  \
      |                   ^~~~~~~
/tmp/main.cpp:16:35: note: in expansion of macro ‘cbrt’
   16 |     printf("cbrtf(3.375) = %f\n", cbrt(y)); // converts const float to
float,
      |                                   ^~~~
/tmp/main.cpp:8:21: error: expected primary-expression before ‘float’
    8 |                     float: cbrtf  \
      |                     ^~~~~
/tmp/main.cpp:16:35: note: in expansion of macro ‘cbrt’
   16 |     printf("cbrtf(3.375) = %f\n", cbrt(y)); // converts const float to
float,
      |                                   ^~~~
```

Reply via email to