A very simple question: are there any guarantees that a C int
is the same thing as a C++ int
, on the same system?
It goes without saying that this is, of course, a purely theoretical question.
The C and C++ standards use the same language to define the fundamental types. But whereas Fortran 2003 makes it clear that
use ISO_C_BINDING
integer(kind=c_int) :: i
declares an integer type which is compatible with the int
type on a "companion C processor", I can't find any such assertion in the C++ stardard. It seems very odd that Fortran would provide stronger C interoperability guarantees than C++!
The closest I can find is section 7.5 [dcl.link], paragraph 3 of the C++11 standard, which states that
Every implementation shall provide for linkage to functions written in the C programming language
But this little sentence doesn't (to me) seem strong enough to guarantee compatibility of fundamental types.
Is there some other language in the C++ standard that I've overlooked which guarantees this, or is it just so obviously taken for granted that no-one has bothered to state it explicitly?
EDIT: David Schwartz in the comments points out that I was imprecise when I said "the same system". I really meant the same "platform", i.e. hardware, OS, system libraries etc. It's really an ABI issue of course. In the quoted passage the C++ standard seems to want to indicate that you can call C functions with extern "C"
, but I'm not sure if it provides enough other guarantees?
The signed and unsigned integer types shall satisfy the constraints given in the C standard, section 5.2.4.2.1.
? – Candlenutbool
/_Bool
and standard-layout structs... – Mellisaint
than a specific C compiler (with specific command line args etc.). – Baselextern "C"
should let you call C functions -- but I'm not sure if it actually does. – Mellisa