LWN.net Logo

Day: GNOME OS

Day: GNOME OS

Posted Aug 10, 2012 10:57 UTC (Fri) by jwakely (subscriber, #60262)
In reply to: Day: GNOME OS by mathstuf
Parent article: Day: GNOME OS

If C++ hadn't supported C casts (or had used a FFI for C interop, as you suggested above) it would never have achieved the success it enjoys today, which only happened by building on C's popularity and success. C compatibility is sometimes a millstone around the neck of C++ now, but without it this discussion probably wouldn't even be happening.

Without supporting C-style casts you couldn't write something as simple as this and have it be valid in both C and C++:

  int* p = (int*)malloc(n*sizeof(int));
It's the right design for a C-style cast to invoke a user-defined conversion if one exists, for the reasons nix gives. Why would you want to use C-style casts in templates anyway? Code in templates doesn't need to be C-compatible. You can't cast between arbitrary POD types anyway, in C or C++. Can you give an example that shows what you mean?


(Log in to post comments)

Day: GNOME OS

Posted Aug 10, 2012 11:09 UTC (Fri) by hummassa (subscriber, #307) [Link]

You can't c-cast from a POD type to another, but you can c-cast from a pointer-to-any-type to another (even if you have to cast to void* in the middle).

Day: GNOME OS

Posted Aug 10, 2012 11:18 UTC (Fri) by jwakely (subscriber, #60262) [Link]

Was mathstuf's statement "it acts differently based on whether the template type is POD or not" talking about casting pointers?

Day: GNOME OS

Posted Aug 10, 2012 14:59 UTC (Fri) by Cyberax (✭ supporter ✭, #52523) [Link]

You can always do:
>int *p = reinterpret_cast<int*>(malloc(n*sizeof(int))

Day: GNOME OS

Posted Aug 10, 2012 15:51 UTC (Fri) by jwakely (subscriber, #60262) [Link]

How would that be valid C?

Day: GNOME OS

Posted Aug 10, 2012 16:54 UTC (Fri) by Cyberax (✭ supporter ✭, #52523) [Link]

It's not, and that's the point. C++ could have avoided lots of warts caused by C-compatibility without losing in functionality.

Copyright © 2013, Eklektix, Inc.
Comments and public postings are copyrighted by their creators.
Linux is a registered trademark of Linus Torvalds