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?

Tristan Brindle
  • 16,281
  • 4
  • 39
  • 82
  • 1
    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) – Uli Köhler Jan 29 '14 at 03:53
  • What does "the same system" mean? Same hardware? Same OS? Same compiler suite? Same system libraries? – David Schwartz Jan 29 '14 at 03:55
  • 2
    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.` ? – Jesse Good Jan 29 '14 at 04:00
  • @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... – Tristan Brindle Jan 29 '14 at 04:06
  • @TristanBrindle: That part was added due to [defect report 483](http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2012/n3383.html). You are right about other fundamental types. I don't think the C standard makes any requirements for floating point types though. – Jesse Good Jan 29 '14 at 04:12
  • 2
    @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.). – Tony Delroy Jan 29 '14 at 04:20
  • @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. – Tristan Brindle Jan 29 '14 at 04:27

1 Answers1

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.

Yakk - Adam Nevraumont
  • 262,606
  • 27
  • 330
  • 524
  • 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. – david.pfx Jan 30 '14 at 13:02