Slashdot Mirror


GCC Switches From C to C++

According to a post on the GNU Compiler Collection list, GCC is now built as a C++ program by default. This is the fruition of much effort, and the goal now is to clean up the GCC internals that are reportedly pretty type-unsafe by rewriting them using C++ classes and templates.

10 of 406 comments (clear)

  1. Re:too bad GCC is not relevant anymore thanks to L by DeathToBill · · Score: 5, Informative

    Irrelevant? Not quite. For your particular use, maybe. But most Linux distros are still built using GCC, and most embedded platforms provide a GCC-based toolchain. So if, by 'irrelevant', you actually mean, 'the compiler with the most-often executed output code on earth', then yes, I guess you're right.

    --
    Slashdot - News for Nerds, Stuff that Matters, in ISO-8859-1 Has just realised that beta makes this signature redundant
  2. Compiler bootstrapping? by Ynot_82 · · Score: 5, Interesting

    How will this affect bootstrapping the GCC on bare systems?

    Been a while since I've delved into LFS or the like, but I'd think GCC being C++ based would seriously complicate things as it's now got more dependencies.

  3. More like "C with Classes" by Megane · · Score: 5, Informative

    I've read their guidelines, and they're doing much like I've been doing recently with moving from C to C++ for embedded systems programming, which is to avoid the really crazy shit that you can do in C++. In particular, exceptions and RTTI are absolutely verboten. They're even planning a compiler switch that turns off the features that will be outlawed in the compiler source. Any templates outside of STL are also forbidden ("template hell" sucks), and I won't even use STL myself because I can't count on having a heap. Even iostreams are being frowned on except maybe possibly in debug dump code where no text language translations are needed.

    C++ can really tidy up C code that uses any sort of function pointer hooks or object dispatch style switch statements with virtual methods. A class can become a mini-API, and even used as a sort of device-driver, as in the mbed libraries. Doing this has really helped improve encapsulation in my own code.

    --
    #naabhaprzrag, #sverubfr-000, #agi-fcbafberq, negvpyr[pynff*=' negvpyr-ary-'] { qvfcynl: abar !vzcbegnag; }
    1. Re:More like "C with Classes" by Aidtopia · · Score: 5, Informative

      In particular, exceptions and RTTI are absolutely verboten.

      Ignoring RTTI is fine, but forbidding exceptions requires a dangerous sort of doublethink. The language itself, as well as the STL, is defined to generate and use exceptions. By ignoring their existence, you banish yourself to a nonstandard purgatory.

      For example, every new now must become new(std::nothrow). For every STL container type, you have to provide a custom allocator that doesn't throw. That's a bit unwieldy.

      By denying exceptions, you force everyone to use error-prone idioms. For example, the only way a constructor can signal a failure is to throw an exception. If you forbid exceptions, then all constructors must always be failure-proof. And then you have to provide an extra initializer method to do the real initialization that can fail. Every user of the class must reliably call the init method after construction, which gets cumbersome when classes are nested or when you're putting instances into an STL container. It also means that objects can have a zombie state--that period of time between construction and initialization. Zombie states add complexity and create an explosion in the number of test cases. Separate initialization means you can't always use const when you should.

      Exceptions are necessary to the C++ promise that your object will be fully constructed and later destructed, or it will not be constructed at all. This is the basis of the RAII pattern, which just happens to be the best pattern yet devised for resource management. Without RAII, you will almost certainly have leaks. Worse, you won't be able to write exception-safe code, so you are essentially closing the door to ever using exceptions.

  4. Re:Classes/Templates are not a magic bullet ... by serviscope_minor · · Score: 5, Informative

    Given a collection of developers that write difficult to understand, difficult to maintain and sloppy type unsafe code, going to C++ may not help.

    It is very difficult to write easy to understand, type-safe, code in C.

    The reason being that C requires so much micro-management that you end up with the code for that mixed in with the actual interesting algorithms. C++ basically makes the compiler do an awful lot of what you have to do in C anyway and does it for you automatically while keeping the details neatly out of the way.

    It's also very hard to write type safe code properly in C. Just look at the classic example of the unsafe qsort versus the safer and faster std::sort.

    I'd also be more optimistic if by using classes and templates they were really referring to using STL, not writing their own.

    What on earth is wrong with writing your own classes and templates? They almost certainly already have a healthy collection of structs with function pointers and macros (linux certainly does have poor reimplementations of half of C++ using macros). These are best replaced with classes and templates on the whole.

    That's the point. C++ formalises what everyone was doing in C anyway, making it much more uniform, easier to read, shorter and therefore much less prone to bugs.

    --
    SJW n. One who posts facts.
  5. Re:GCC should remain small and fast by serviscope_minor · · Score: 5, Insightful

    I used to be more polite to ignorant C++ haters. But I've lost patience.

    A program written in C++ is going to be slower than an equivalent program written in C, no way around it.

    There is a way around it: by not being an astonishingly incompetent developer.

    And that's without even getting into features like templates, which only five people in the world understand.

    Don't assume everyone is as dim as you.

    --
    SJW n. One who posts facts.
  6. C++ is Dead, Long Live Java by Anonymous Coward · · Score: 5, Funny

    C++ is as good as dead.

    Everything should be written in java, since this would give a huge speed increase.

    For even more speed, programs could be run on a java interpreter, running on a java interpreter written in java.

    Think of the raw speed!

  7. Re:Classes/Templates are not a magic bullet ... by serviscope_minor · · Score: 5, Insightful

    You can do all kinds of nifty stuff with macros and gcc/clang extensions to provide type safety to C

    Yes, I know.

    You can write a GENERATE_SORT(Type, Comparator) macro which generates a sort function to work on an array of Type, using the specified comparator, and has no name collisions and is type safe using liberal amounts of ## and so on.

    The point is not that you can't do them in C (you can), but the methods for doing it are ad-hoc. By moving the functionality into the compiler, C++ provides a regularity of syntax for such things that C lacks.

    --
    SJW n. One who posts facts.
  8. Re:Classes/Templates are not a magic bullet ... by serviscope_minor · · Score: 5, Insightful

    I don't write 'university code' though; and I'm not out to impress a professor or win 'elegance awards'.

    I think the enormous chip on your shoulder might be obscuring your view of C++.

    --
    SJW n. One who posts facts.
  9. Re:Major *nix app using C++ by formfeed · · Score: 5, Funny

    Linus must be screaming inside.

    Linus never allows his screaming to stay on the inside