public inbox for gcc-bugs@sourceware.org
help / color / mirror / Atom feed
* [Bug c++/108965] New: g++: unable to parse c11 _Generics
@ 2023-02-28 10:03 chrisfriedt at gmail dot com
  2023-02-28 10:20 ` [Bug c++/108965] " redi at gcc dot gnu.org
                   ` (10 more replies)
  0 siblings, 11 replies; 12+ messages in thread
From: chrisfriedt at gmail dot com @ 2023-02-28 10:03 UTC (permalink / raw)
  To: gcc-bugs

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,
      |                                   ^~~~
```

^ permalink raw reply	[flat|nested] 12+ messages in thread

end of thread, other threads:[~2023-02-28 13:34 UTC | newest]

Thread overview: 12+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2023-02-28 10:03 [Bug c++/108965] New: g++: unable to parse c11 _Generics chrisfriedt at gmail dot com
2023-02-28 10:20 ` [Bug c++/108965] " redi at gcc dot gnu.org
2023-02-28 10:35 ` redi at gcc dot gnu.org
2023-02-28 10:54 ` chrisfriedt at gmail dot com
2023-02-28 11:02 ` pinskia at gcc dot gnu.org
2023-02-28 11:50 ` redi at gcc dot gnu.org
2023-02-28 12:28 ` chrisfriedt at gmail dot com
2023-02-28 12:37 ` redi at gcc dot gnu.org
2023-02-28 12:40 ` chrisfriedt at gmail dot com
2023-02-28 12:48 ` redi at gcc dot gnu.org
2023-02-28 12:58 ` chrisfriedt at gmail dot com
2023-02-28 13:34 ` chrisfriedt at gmail dot com

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).