extern "C" function return types
Jonathan Wakely
jwakely.gcc@gmail.com
Wed Feb 23 23:30:00 GMT 2011
On 23 February 2011 18:45, Brian Kohan wrote:
> If you want to use C++ code from code compiled by a C compiler its
> obvious that the return type of the externed function must be a valid
> C type (no templates, classes, etc). However, another usage of
> external C linkage is to allow for symbol discovery via a call to
> dlsym(). In this case more often than not C++ code is calling the
> externed function and will be able to understand C++ specific return
> types. For example:
>
> extern "C" {
> list<string> getString() {};
> }
>
> The above code compiles and runs fine with gcc. In other compilers,
> namely msvc, the above code does not compile because the return type
> is not valid C. I've read through the C++ specification and can't find
> any statement regarding requirements on return types of extern C
> functions. Most online documentation will say one way or the other,
> usually depending on if its a Unix or MS source.
>
> Is there an official ANSI/ISO C++ stance that I've missed?
There is nothing in the standard saying 'you cannot do X, Y, Z in an
extern "C" block' so there is no such restriction.
It absolutely does not mean "code inside this block is C code"
> Is it reasonable to expect that the above code will always work in gcc?
Yes. MSVC is wrong to reject it.
More information about the Gcc-help
mailing list