Path: csiph.com!eternal-september.org!reader02.eternal-september.org!.POSTED!not-for-mail From: Tim Rentsch Newsgroups: comp.lang.c++ Subject: Re: Can extern "C" functions throw exceptions Date: Thu, 16 Dec 2021 04:37:17 -0800 Organization: A noiseless patient Spider Lines: 72 Message-ID: <86ee6c4tfm.fsf@linuxsc.com> References: <20210912144443.a174f80b419b5694959f26d9@cvine--nospam--.freeserve.co.uk> <86a6jlrjyd.fsf@linuxsc.com> <9106335c-2f48-4735-8c69-2b829fc450ccn@googlegroups.com> <868rwm6adq.fsf@linuxsc.com> <2014666f-87aa-4eb6-8fc0-f0d0addd5011n@googlegroups.com> Mime-Version: 1.0 Content-Type: text/plain; charset=us-ascii Injection-Info: reader02.eternal-september.org; posting-host="e672fb9610cb67396dff88ffd2617ec0"; logging-data="11986"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX18iQ8gmkn9RtafXFr1c/tZPWUpLBrqlzn8=" User-Agent: Gnus/5.11 (Gnus v5.11) Emacs/22.4 (gnu/linux) Cancel-Lock: sha1:CktGSekRLFv1+jURDesEIdR/59k= sha1:ESmSw5wA4uc1rhwzUQcfUjCpCNI= Xref: csiph.com comp.lang.c++:82656 Ian Collins writes: > On 15/12/2021 16:06, james...@alumni.caltech.edu wrote: > >> On Tuesday, December 14, 2021 at 6:21:38 PM UTC-5, Tim Rentsch wrote: >> >>> "james...@alumni.caltech.edu" writes: >> >> ... >> >>>> The language linkage of a function's type is indeed part of >>>> that type. The language linkage of a function's name is not. >>>> The standard's description of language linkage in 9.11p1 starts >>>> with the sentence "All function types, function names with >>>> external linkage, and variable names with external linkage have >>>> a language linkage.", clearly making the point that the >>>> language linkage of a function's type is a distinct thing from >>>> the language linkage of a function's name. The standard could >>>> have inextricably linked them together - but it does not. In >>>> fact, 9.11p5 includes an example demonstrating how they can be >>>> separated: >>>> >>>> extern "C" typedef void FUNC(); >>>> FUNC f2; // the name f2 has C ++ language linkage and the >>>> // function?s type has C language linkage >>>> >>>> [...] >>> >>> Thank you for this citation. It doesn't answer all my questions >>> but it certainly does provide an illuminating example. >> >> If you can figure out words to express your remaining questions, >> I'd be curious to find out what they are. >> >> It appears, however, that in practice there is no difference >> >>> between a type with a C language linkage and a C++ language >>> linkage. Apparently most compilers ignore the distinction >>> because following the rules would break too much code. Here >>> is a link to stackoverflow with more information: >>> >>> https://stackoverflow.com/questions/26647259/ >>> is-extern-c-a-part-of-the-type-of-a-function >> >> The standard is quite clear: "Two function types with different >> language linkages are distinct types even if they are otherwise >> identical." (9.11p1). I'm annoyed, but unfortunately not >> surprised, to find that this has been ignored. I've never had >> any need to write code where that issue would matter, which is >> why I've never noticed. > > I have only used one compiler, Sun CC, that bothered to enforce > the rule. At the time, the main problem this caused me was it > prevented static member functions being passed to pthread_create, > I had to use extern "C" qualified friend function on Solaris. Did you try a pattern like this: extern "C" typedef void Whatsit( double * ); class Foo { public: static Whatsit fred; }; void Foo::fred( double *things ){ for( int i = 0; i < 10; i++ ) things[i] += i; } This code is accepted by g++ back to -std=c++98. Of course, since we know the gnu tools are compromised that doesn't tell us very much...