Question

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?

Was it helpful?

Solution

No.

There are widely used conforming compilers on x64 amd compatible cpus that treat longas 32 bit and others as 64 bit by default. So this is not even the case for two C++ compilers on the same system, let alone a C++ and C compiler.

Within one compiler, that is up to the compiler vendor if they are compatible. They usually (always) are. "one compiler" is a bit of a misnomer here: the C snd C++ compilers are different compilers, even if in the same binary by the same vendor, in a sense.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top