public inbox for gcc-bugs@sourceware.org
help / color / mirror / Atom feed
From: "redi at gcc dot gnu.org" <gcc-bugzilla@gcc.gnu.org>
To: gcc-bugs@gcc.gnu.org
Subject: [Bug c++/2316] g++ fails to overload on language linkage
Date: Tue, 30 Aug 2011 10:53:00 -0000	[thread overview]
Message-ID: <bug-2316-4-H05SZ3mzkl@http.gcc.gnu.org/bugzilla/> (raw)
In-Reply-To: <bug-2316-4@http.gcc.gnu.org/bugzilla/>

http://gcc.gnu.org/bugzilla/show_bug.cgi?id=2316

--- Comment #23 from Jonathan Wakely <redi at gcc dot gnu.org> 2011-08-30 10:46:51 UTC ---
(In reply to comment #22)
> Note that some things are painful to do right with extern "C". For instance,
> the __stoa helper takes as argument a pointer to a function with a type that
> depends on template arguments. As far as I can see, it is not possible to do
> that for extern "C" functions (I posted a question about that on clc++m
> yesterday), so that would mean rewriting this code differently.

I think you can do it with a alias-declaration in an extern "C" block:

extern "C" {
  template<typename T>
    using func_type = T (*)();
}

extern "C" void c() { }

template<typename T>
  void f( func_type<T> p ) { p(); }

int main()
{
  f(&c);
}

But yes, it's a bit of a hole in the type system that language linkage is part
of the type but can only be specified in some contexts and can't be deduced by
templates.  We could solve it with an alternative syntax for language linkage
using attributes:

  void f( [[extern(C)]] void (*p)() );

could be equivalent to:

  extern "C" {  typedef void (*func_type)();  }
  void f( func_type p );

so we could say:

  template<typename T, typename U>
    void g( [[extern(C)]] T (*p)(U));


Without alias-declarations, the tedious way to fix __stoa would be write an
extern "C++" forwarding function for each of the strtol, stroul, vsnprintf etc.
functions we need, and pass that forwarding function to the helper function
templates. That could be done with macros, and would still be simpler than
re-implementing the __stoa error-checking logic in each one.  Anyway, that is
probably straying off-topic for this PR.

> My guess is
> that making extern "C" functions usable would require a few DR, or extern "C"
> being part of the type will follow the example of "export" and be
> deprecated/removed (the second option is more likely, even if that's yet
> another change that hurts Oracle's compiler).

Clang++ and VC++ also "implement" this bug.  If it's fixed in G++ it would need
to be controlled by a switch, maybe with the current behaviour as the default
(but issuing a warning) for a couple of releases. As I said in c13, it could
break a lot of code, plenty of people don't even know that they shouldn't be
able to pass extern "C++" functions to C library APIs.


  parent reply	other threads:[~2011-08-30 10:48 UTC|newest]

Thread overview: 49+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
     [not found] <bug-2316-4@http.gcc.gnu.org/bugzilla/>
2011-08-30  1:17 ` marc.glisse at normalesup dot org
2011-08-30  9:31 ` redi at gcc dot gnu.org
2011-08-30  9:40 ` marc.glisse at normalesup dot org
2011-08-30 10:53 ` redi at gcc dot gnu.org [this message]
2011-08-30 11:32 ` redi at gcc dot gnu.org
2011-08-30 11:43 ` marc.glisse at normalesup dot org
2011-08-30 12:26 ` redi at gcc dot gnu.org
2011-08-30 12:35 ` redi at gcc dot gnu.org
2011-08-30 13:20 ` marc.glisse at normalesup dot org
2011-08-30 19:34 ` marc.glisse at normalesup dot org
2011-08-30 20:48 ` marc.glisse at normalesup dot org
2011-08-31 14:15 ` marc.glisse at normalesup dot org
2011-09-03 13:02 ` marc.glisse at normalesup dot org
2011-09-04 11:07 ` marc.glisse at normalesup dot org
2011-09-15 17:13 ` marc.glisse at normalesup dot org
2012-01-02  0:19 ` marc.glisse at normalesup dot org
2012-01-02  1:05 ` redi at gcc dot gnu.org
2012-01-02  2:33 ` pinskia at gcc dot gnu.org
2012-01-02 10:40 ` marc.glisse at normalesup dot org
2012-01-04 11:14 ` marc.glisse at normalesup dot org
2012-01-04 11:54 ` redi at gcc dot gnu.org
2012-01-04 12:28 ` bangerth at gmail dot com
2012-01-04 12:48 ` jakub at gcc dot gnu.org
2012-01-04 12:54 ` marc.glisse at normalesup dot org
2012-01-04 13:00 ` redi at gcc dot gnu.org
2012-01-04 13:06 ` marc.glisse at normalesup dot org
2012-02-22 12:56 ` xiaoyuanbo at yeah dot net
2014-03-01 18:17 ` harald at gigawatt dot nl
2014-03-01 18:52 ` glisse at gcc dot gnu.org
2014-03-02 15:41 ` amylaar at gcc dot gnu.org
2014-03-08  1:36 ` harald at gigawatt dot nl
2014-03-08  8:16 ` glisse at gcc dot gnu.org
2014-04-21 19:39 ` harald at gigawatt dot nl
2021-12-14 22:50 ` eric-bugs at omnifarious dot org
     [not found] <bug-2316-1186@http.gcc.gnu.org/bugzilla/>
2006-07-07 20:53 ` pinskia at gcc dot gnu dot org
2006-09-12 16:55 ` pinskia at gcc dot gnu dot org
2006-10-10 16:34 ` pinskia at gcc dot gnu dot org
2006-10-10 20:07 ` bkoz at gcc dot gnu dot org
2006-10-15  3:25 ` gcc-bugzilla at kayari dot org
2006-10-15  3:28   ` Andrew Pinski
2006-10-15  3:29 ` pinskia at gmail dot com
2006-12-04 15:54 ` marc dot glisse at normalesup dot org
2009-06-04 14:36 ` pinskia at gcc dot gnu dot org
2009-06-04 14:50 ` paolo dot carlini at oracle dot com
2009-06-05  4:23 ` marc dot glisse at normalesup dot org
2009-11-12  4:55 ` jason at gcc dot gnu dot org
     [not found] <20010318174600.2316.sebor@roguewave.com>
2005-09-09 12:56 ` rguenth at gcc dot gnu dot org
2005-09-09 14:27 ` gdr at integrable-solutions dot net
2005-09-11 21:34 ` sebor at roguewave dot com

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=bug-2316-4-H05SZ3mzkl@http.gcc.gnu.org/bugzilla/ \
    --to=gcc-bugzilla@gcc.gnu.org \
    --cc=gcc-bugs@gcc.gnu.org \
    /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).