public inbox for gcc-help@gcc.gnu.org
 help / color / mirror / Atom feed
From: Xi Ruoyao <xry111@xry111.site>
To: Jonathan Wakely <jwakely.gcc@gmail.com>, Pepe <list.gcc-help@pe82.de>
Cc: gcc-help@gcc.gnu.org
Subject: Re: Correct way to provide a C callback function nside C++
Date: Fri, 27 Jan 2023 22:30:33 +0800	[thread overview]
Message-ID: <fb4f716dec87539b86deee8f87362c250647ca88.camel@xry111.site> (raw)
In-Reply-To: <CAH6eHdQqJ3-Ac3XQTP6+Rdt3PxXj944hYPwJ-TCZhQTKxKyV=Q@mail.gmail.com>

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.

> The relevant GCC bug about this nonconformance is
> https://gcc.gnu.org/bugzilla/show_bug.cgi?id=2316 (and will probably
> never be fixed, because it would break far too much code).

Perhaps we should document it as an extension as well and add a warning
option...  But this will be the lowest priority even if we'd spend our
time for the job.
-- 
Xi Ruoyao <xry111@xry111.site>
School of Aerospace Science and Technology, Xidian University

  parent reply	other threads:[~2023-01-27 14:30 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 [this message]
2023-01-27 15:32     ` Jonathan Wakely

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=fb4f716dec87539b86deee8f87362c250647ca88.camel@xry111.site \
    --to=xry111@xry111.site \
    --cc=gcc-help@gcc.gnu.org \
    --cc=jwakely.gcc@gmail.com \
    --cc=list.gcc-help@pe82.de \
    /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).