public inbox for gcc-help@gcc.gnu.org
 help / color / mirror / Atom feed
From: Jonathan Wakely <jwakely.gcc@gmail.com>
To: Xi Ruoyao <xry111@xry111.site>
Cc: Pepe <list.gcc-help@pe82.de>, gcc-help@gcc.gnu.org
Subject: Re: Correct way to provide a C callback function nside C++
Date: Fri, 27 Jan 2023 15:32:41 +0000	[thread overview]
Message-ID: <CAH6eHdRSsz7tkC+od6hobiRjXbSjBC=-48dVreZ6PuGAd2UwAg@mail.gmail.com> (raw)
In-Reply-To: <fb4f716dec87539b86deee8f87362c250647ca88.camel@xry111.site>

On Fri, 27 Jan 2023 at 14:30, Xi Ruoyao wrote:
>
> On Fri, 2023-01-27 at 14:07 +0000, Jonathan Wakely via Gcc-help wrote:
> > I think it's safe to assume that *either* the code compiles and works
> > as expected, or fails to compile. And in practice it compiles and
> > works with all widely used compilers. You will not find a C++
> > implementation where the types are not compatible, but the code
> > compiles anyway and then misbehaves at runtime.
>
> FWIW if we do "some strange thing" we may end up shooting our own foot.
> Like:
>
> a.cc:
>
> #include <cstdio>
>
> extern "C"
> {
>         struct A { struct {} x; int a; };
>         void callback(void (*fn)(struct A *));
> }
>
> void f(struct A *p)
> {
>         std::printf("%d\n", p->a);
> }
>
> int main()
> {
>         callback(f);
> }
>
> b.c:
>
> #include <stdio.h>
>
> struct A { struct {} x; int a; };
> void callback(void (*fn)(struct A *))
> {
>         struct A foo = { .a = 42 };
>         fn(&foo);
> }
>
> This thing won't work with GCC because struct A will have different
> layouts in GNU C and C++.  Note that the C standard does not allow an
> empty struct at all (a pedantic C compiler should reject b.c as an empty
> struct violates the syntax rule of C).  But GNU C supports it as an
> extension.

Strictly speaking, a pedantic C compiler should issue a diagnostic. It
doesn't have to fail to compile.

$ gcc -pedantic b.c
b.c:3:12: warning: struct has no members [-Wpedantic]
   3 | struct A { struct {} x; int a; };
     |            ^~~~~~

      reply	other threads:[~2023-01-27 15:32 UTC|newest]

Thread overview: 7+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2023-01-27 13:23 Pepe
2023-01-27 13:48 ` Tom Kacvinsky
2023-01-27 13:51   ` Jonathan Wakely
2023-01-27 14:07 ` Jonathan Wakely
2023-01-27 14:29   ` Michael Pape
2023-01-27 14:30   ` Xi Ruoyao
2023-01-27 15:32     ` Jonathan Wakely [this message]

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to='CAH6eHdRSsz7tkC+od6hobiRjXbSjBC=-48dVreZ6PuGAd2UwAg@mail.gmail.com' \
    --to=jwakely.gcc@gmail.com \
    --cc=gcc-help@gcc.gnu.org \
    --cc=list.gcc-help@pe82.de \
    --cc=xry111@xry111.site \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
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).