Interoperability of types in C and C++
Asked Answered
M

1

7

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?

Mellisa answered 29/1, 2014 at 3:51 Comment(7)
I'd extend on the same system to on the same system with the same compiler and compiler settings. Otherwise the answer will definitely be no (you can compile a i386 binary with 32-bit int on a 64-bit system)Suppletory
What does "the same system" mean? Same hardware? Same OS? Same compiler suite? Same system libraries?Bottomry
Perhaps 3.9.1p3 The signed and unsigned integer types shall satisfy the constraints given in the C standard, section 5.2.4.2.1. ?Candlenut
@JesseGood That sentence doesn't appear in the C++11 draft I have (n3337) -- I guess I need to find a newer one. That seems to cover it I think -- but leaves open the question of floating point types, bool/_Bool and standard-layout structs...Mellisa
@TristanBrindle: That part was added due to defect report 483. You are right about other fundamental types. I don't think the C standard makes any requirements for floating point types though.Candlenut
@JesseGood: 5.2.4.2.1 is phrased in terms of minimum ranges per "shall be equal or greater in magnitude"... a C++ compiler could satisfy those requirements having used a wider or narrower int than a specific C compiler (with specific command line args etc.).Basel
@DavidSchwartz Good point. I really meant the same "platform", i.e. hardware, OS, system libraries etc. It's really an ABI issue of course, and C++ seems to want to indicate that extern "C" should let you call C functions -- but I'm not sure if it actually does.Mellisa
A
5

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.

Atrip answered 29/1, 2014 at 4:12 Comment(1)
The answer is correct. The C and C++ standards use similar language, and clearly make no guarantees that any integers are any particular size. I do not believe the Fortran compiler can make good on its promise either, for similar reasons.Frock

© 2022 - 2024 — McMap. All rights reserved.