Re: extern "C" function return types

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



On 02/23/2011 01:08 PM, Jonathan Wakely wrote:
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.

I have code that returns an "std::list<class foo *> *" from a function of this type and it works fine in both GCC 4.3+ and VS2008/2010.

I think the issue here is that you can't return an object, but you can return a pointer to an object. I think I tried returning the list as an object (by value) and failed... but I'm not sure.

--
Kevin P. Fleming
Digium, Inc. | Director of Software Technologies
445 Jan Davis Drive NW - Huntsville, AL 35806 - USA
skype: kpfleming | jabber: kfleming@xxxxxxxxxx
Check us out at www.digium.com & www.asterisk.org


[Index of Archives]     [Linux C Programming]     [Linux Kernel]     [eCos]     [Fedora Development]     [Fedora Announce]     [Autoconf]     [The DWARVES Debugging Tools]     [Yosemite Campsites]     [Yosemite News]     [Linux GCC]

  Powered by Linux