Topic: extern "C" (was C9X vs C++)


Author: David R Tribble <david@tribble.com>
Date: 1999/10/27
Raw View
Robert J Macomber wrote:
>
> In article <380E4C79.E64E55E8@tribble.com>,
> David R Tribble  <david@tribble.com> wrote:
> >Robert J Macomber wrote:
> >>    extern "C" void foo() { throw 0; }
> >>    int main() { foo(); }
>
> >Since extern C functions are still C++ functions (but simply having
> >different linkage), the C++ statements within them should act like
> >normal C++ statements.  Which includes throw(), new(), etc.
> >
> >This seems to be a common misunderstanding of what 'extern "C"' does;
> >it does not turn C++ code into C code.  Perhaps this should be in the
> >C++ FAQ?
>
> I know that; this code:
>
> extern "C" void foo() {
>   try { throw 1; }
>   catch(...) {}
> }
>
> and this:
>
> void thrower() { throw 1; }
> extern "C" catcher() {
>   try { thrower(); }
>   catch(...) {}
> }
>
> are without a doubt perfectly well-defined.  What I'm asking is
> whether propagation of exceptions *out* of a C-linkage function (even
> directly into another C++ function) has defined behavior.  Presumably,
> exceptions require some support from a system's calling conventions,
> and 'extern "C"' can change those, correct?

Oh, I see your point now.  You're asking if extern C functions can
have an exception-supporting invocation frame and simultaneously have
a C-only invocation frame.

That's a good question; it seems to me that the caller of an extern
C function can't know whether it will throw or not (without a
'throws' clause, anyway), or whether it really is C code that's
being called (which presumably does not use an exception-supporting
invocation frame).

Perhaps for it to work portably (if at all), the extern C function
declaration should be required to have a 'throws' clause, so that
the compiler can make the proper assumptions.

-- David R. Tribble, david@tribble.com, http://www.david.tribble.com --
---
[ comp.std.c++ is moderated.  To submit articles, try just posting with ]
[ your news-reader.  If that fails, use mailto:std-c++@ncar.ucar.edu    ]
[              --- Please see the FAQ before posting. ---               ]
[ FAQ: http://reality.sgi.com/austern_mti/std-c++/faq.html              ]





Author: rmacombe@lucia.u.arizona.edu (Robert J Macomber)
Date: 1999/10/22
Raw View
In article <380E4C79.E64E55E8@tribble.com>,
David R Tribble  <david@tribble.com> wrote:
>Robert J Macomber wrote:
>>    extern "C" void foo() { throw 0; }
>>    int main() { foo(); }

>Since extern C functions are still C++ functions (but simply having
>different linkage), the C++ statements within them should act like
>normal C++ statements.  Which includes throw(), new(), etc.
>
>This seems to be a common misunderstanding of what 'extern "C"' does;
>it does not turn C++ code into C code.  Perhaps this should be in the
>C++ FAQ?

I know that; this code:

extern "C" void foo() {
  try { throw 1; }
  catch(...) {}
}

and this:

void thrower() { throw 1; }
extern "C" catcher() {
  try { thrower(); }
  catch(...) {}
}

are without a doubt perfectly well-defined.  What I'm asking is
whether propagation of exceptions *out* of a C-linkage function (even
directly into another C++ function) has defined behavior.  Presumably,
exceptions require some support from a system's calling conventions,
and 'extern "C"' can change those, correct?
--
   Rob Macomber
    (rmacombe@u.arizona.edu)
---
[ comp.std.c++ is moderated.  To submit articles, try just posting with ]
[ your news-reader.  If that fails, use mailto:std-c++@ncar.ucar.edu    ]
[              --- Please see the FAQ before posting. ---               ]
[ FAQ: http://reality.sgi.com/austern_mti/std-c++/faq.html              ]





Author: David R Tribble <david@tribble.com>
Date: 1999/10/21
Raw View
Robert J Macomber wrote:
>
> David R Tribble  <david@tribble.com> wrote:
>> Other compilers (e.g., IBM OS/390 USS) will complain, and rightly so,
>> about incompatible function pointers.  Some systems use different
>> function invocation methods for C and C++ functions.  Hence the
>> need for the extern "C" specifier.
>
> Speaking of which, does this code have defined behavior?
>
>    extern "C" void foo() { throw 0; }
>    int main() { foo(); }
>
> I can't find anything in the standard which says that exceptions
> should or should not propagate properly through a C++ function that
> happens to have C linkage.

Since extern C functions are still C++ functions (but simply having
different linkage), the C++ statements within them should act like
normal C++ statements.  Which includes throw(), new(), etc.

This seems to be a common misunderstanding of what 'extern "C"' does;
it does not turn C++ code into C code.  Perhaps this should be in the
C++ FAQ?

-- David R. Tribble, david@tribble.com, http://www.david.tribble.com --
---
[ comp.std.c++ is moderated.  To submit articles, try just posting with ]
[ your news-reader.  If that fails, use mailto:std-c++@ncar.ucar.edu    ]
[              --- Please see the FAQ before posting. ---               ]
[ FAQ: http://reality.sgi.com/austern_mti/std-c++/faq.html              ]





Author: Hyman Rosen <hymie@prolifics.com>
Date: 1999/10/15
Raw View
rmacombe@lucia.u.arizona.edu (Robert J Macomber) writes:
> Speaking of which, does this code have defined behavior?
> extern "C" void foo() { throw 0; }
> int main() { foo(); }

Yes. The only thing that "C" linkage affects is linkage.
---
[ comp.std.c++ is moderated.  To submit articles, try just posting with ]
[ your news-reader.  If that fails, use mailto:std-c++@ncar.ucar.edu    ]
[              --- Please see the FAQ before posting. ---               ]
[ FAQ: http://reality.sgi.com/austern_mti/std-c++/faq.html              ]





Author: rmacombe@lucia.u.arizona.edu (Robert J Macomber)
Date: 1999/10/14
Raw View
In article <37FE1792.B7E603A1@tribble.com>,
David R Tribble  <david@tribble.com> wrote:
>Other compilers (e.g., IBM OS/390 USS) will complain, and rightly so,
>about incompatible function pointers.  Some systems use different
>function invocation methods for C and C++ functions.  Hence the
>need for the extern "C" specifier.

Speaking of which, does this code have defined behavior?

extern "C" void foo() { throw 0; }
int main() { foo(); }

I can't find anything in the standard which says that exceptions
should or should not propagate properly through a C++ function that
happens to have C linkage.  My guess would be not (and since I expect
most extern "C" functions are used for callbacks from C code, throwing
execeptions from them would be a Bad Idea anyway) but I was just
wondering.
--
   Rob Macomber
    (rmacombe@u.arizona.edu)
---
[ comp.std.c++ is moderated.  To submit articles, try just posting with ]
[ your news-reader.  If that fails, use mailto:std-c++@ncar.ucar.edu    ]
[              --- Please see the FAQ before posting. ---               ]
[ FAQ: http://reality.sgi.com/austern_mti/std-c++/faq.html              ]