Slashdot Mirror


GCC Moving To Use C++ Instead of C

An anonymous reader writes "CodeSourcery's Mark Mitchell wrote to the GCC mailing list yesterday reporting that 'the GCC Steering Committee and the FSF have approved the use of C++ in GCC itself. Of course, there's no reason for us to use C++ features just because we can. The goal is a better compiler for users, not a C++ code base for its own sake.' Still undecided is what subset of C++ to use, as many contributors are experts in C, but novices in C++; there is a call for a volunteer to develop the C++ coding standards."

44 of 546 comments (clear)

  1. Re:Seems odd... by Capena · · Score: 5, Insightful

    how do you get a C++ compiler working on a platform that doesn't have one

    Why not bootstrap using a cross compiler?

  2. Re:C++? by man_of_mr_e · · Score: 3, Insightful

    It's not a horrible language if you take into account it's requirements. C++'s requirements are horrible and make it the monster it is. It has to have all the low-levelness of C with all the high level goodness of a modern OO language. Languages like Java, C#, Ruby, etc.. all have the advantage of not having to be a low-level language as well. While OS's have been written in languages like Pascal (original MacOS for instance and early versions of Windows) those were also largely custom compilers that added low-level functionality to the language.

    So basically, C and C++ are unique in that they are required to be systems languages as well as applications languages. This makes both of them quirky to say the least.

  3. Re:What... by Ckwop · · Score: 3, Informative

    How do you compile a compiler written in the language it compiles...

    Enjoy

  4. Out of the ashes and into C++ by trialcode · · Score: 4, Funny

    Great idea! This will surely help steal back users from LLVM/clang. The reason people are jumping ship is because they want a compiler written in C++, it has nothing to do with performance, licenses and/or features. Just thinking about those crunchy templates, page up and page down, makes my mouth water. I can't even begin to comprehend how they ever got anything done without templates.

    1. Re:Out of the ashes and into C++ by larry+bagina · · Score: 3, Informative

      LLVM is a virtual machine, optimizer, and compiler infrastructure, not a compiler itself. Clang is written in c++ and support C, Objective C, and C++ (c++ support isn't complete but it self compiles and can compile boost and their own stl). gcc-llvm is a fork of gcc that uses llvm for the code generation. There's also dragonegg, which is a plugin for newer versions of gcc to use llvm code generation without any gcc modifications (well, I think there's a small patch involved). Side note, it's curious how they finally allowed plugins.

      --
      Do you even lift?

      These aren't the 'roids you're looking for.

  5. Choices, choices by Cee · · Score: 4, Insightful

    To paraphrase Einstein:

    Make things as simple as possible, but not simpler.

    IMHO, one should use as high level language as possible, but not higher. One should never choose a lower level language than necessary only because it is hard core, the choice has to be based on something more substantial.

    I've met several C programmers having the knee-jerk reaction when they hear the word C++ that it's bloated and slow and hard. And tell me what, they haven't read Stroustrup's FAQ lately. C++ can be very lean and mean indeed. As can C# (which I'm mostly using right now).

    1. Re:Choices, choices by daid303 · · Score: 4, Insightful

      If you want to link to any FAQ, don't forget the FQA about C++ http://yosefk.com/c++fqa/

      Reading it will give you an inside on the many issues you can have with C++. I don't oppose C++, but You Have To Know What You Are Doing (TM). Or else all hell breaks lose. Fixing bad C is doable, fixing bad C++ is the 7th circle of hell.

    2. Re:Choices, choices by serviscope_minor · · Score: 5, Insightful

      The C++ FQA is mostly a bunch of rhetoric and sophistry with a good scattering of half-truths thrown in for good measure. It is a classic propaganda piece as the falsehoods are spread very continuously spread and mixed with truthful pieces. That makes it hard to debunk in a short post as one has to go in to nit-picking detail in order to expose it for the hokum that it really is. Fortunately, you can use your favourite search engine to search tha annals of comp.langg.c++.moderated for such information.

      --
      SJW n. One who posts facts.
    3. Re:Choices, choices by murdocj · · Score: 5, Interesting

      I spent a lot of years developing in C, some time in C++ (w/o using the standard template library) and the last year and a half using C++ with stl.  So I think I have a pretty valid basis for comparison, and I'd say that C++ has far more ways to go wrong than C does.  With C, you pretty much know what you've got.  C++ has a number of subtle, nasty bear traps that can bite you.  It's true that if you know what you are doing, you can produce good code & get the job done, but that's true of any language, including assembler.

      Here's a couple of items off the top of my head:

      Default assignment operator: All you need to do is add a pointer to your class and suddenly code that you don't see causes a bug.  Yes, IF you know about this you can work around it.  That's true of anything.

      Overloaded operators: I was leafing through the original Stroustrup C++ book and found this paragraph about the stream output operator '<<':
      "But why <<? ... The operators < and > were tried, but the meanings "less than" and "greater than" were so firmly implanted in people's minds that the new I/O statements were for all practical purposes unreadable."

      Well, yes, when people see an operator, they "think" they know what it's doing.  It's interesting to me that in this very first case of overloading, Stroustrup ran into this fundamental problem, and had to choose a somewhat obscure operator to get around it.

      References: references aren't what most people think of as references.  They are pointers with syntactic sugar.  Try getting a reference to an element of a vector, and then doing something that causes the vector storage to be reallocation.  Voila, you have a "reference" that refers to junk.

      All of these aren't impossible problems.  They are extra issues, inherent in the language, that you simply don't have in C.  I think that C++ has a lot of interesting ideas, it has a lot of power, but ultimately it also has a LOT of problems.

  6. Incorrect headline by Letharion · · Score: 5, Informative

    The headline says "Use C++ instead of C" which is incorrect. C++ is, as made obvious from the text, an option, not a requirement.

  7. The devs don't know C++?? Its a C++ compiler! by Viol8 · · Score: 5, Funny

    Are they seriously trying to suggest that the people who work on developing and maintaining a C++ compiler are novices in C++??

    Sorry , am I missing something here?

    1. Re:The devs don't know C++?? Its a C++ compiler! by chocapix · · Score: 5, Informative

      I think you're confused. Strictly speaking, GCC isn't even a C compiler.

      GCC stands for "GNU Compiler Collection". In that collection, there's a C compiler as well as a C++ compiler, a Java compiler, and many more (they are not completely separate, they actually share a lot of code between them). All of them are written in C, and the news here is they're going to be written in C++ in the near future.

    2. Re:The devs don't know C++?? Its a C++ compiler! by dgriff · · Score: 5, Funny

      Are they seriously trying to suggest that the people who work on developing and maintaining a C++ compiler are novices in C++??

      Car analogy time! You can be an expert car mechanic without knowing how to drive.

      I'll get me coat...

  8. Re:C++? by jandersen · · Score: 4, Insightful

    C++'s requirements are horrible and make it the monster it is

    I don't think you are right there. I used to be very sceptical about C++, but I have had to develop some tools with it recently, and my respect for it has grown a good deal.

    It is true that C++ programs can be real horrors to maintain and even to write, but I think the problem often lies with the design of the toolset used. That and the fact that C++ operates on a higher level of abstraction and therefore requires much more careful consideration and planning. The problems I have seen in the past have all been centered around people not quite understanding the nature of C++ and wanting to immediately put all those bright new features to "good" use, by overloading everything and indiscriminately inheriting from any number of classes.

    The secret to good programming has always been to keep it simple - this is twice as important in C++, and the language has some great features for doing so, but you really have to understand what it is you are trying to achieve.

  9. C++ flame wars by o'reor · · Score: 5, Funny
    starting in 3... 2... 1...

    Here's somme ammo from bash.org:

    Phil: C++ is java's uncle that never comes to visit, and had half his face blown off when he stepped on a landmine, also he's a pedophile.
    Phil: But he's the industry standard.
    David: and runs much faster
    Phil: He has to be able to run fast, he's a pedophile.

    --
    In Soviet Russia, our new overlords are belong to all your base.
  10. Safe subset by steveha · · Score: 5, Insightful

    The GCC guys are not going crazy here. They are discussing what subset of C++ to allow.

    If you use all the wild features of C++, the results could be scary. For example, operator overloading is great if used judiciously, but if used badly it can make the code a mess. And if it is used at all, then it means that you can't look at one page from a printout and know for sure what that code does; you need to look at all the class functions to make sure there aren't tricky overloaded operators.

    I use plain C all the time at work, and the top C++ feature they should be using is simply the object-oriented class stuff. With a single global namespace you need to make functions like MyClassAddFloatAndInt(), but in C++ you could just call that function add(); it would be part of MyClass, and if you have other "add" functions with other type signatures, they won't collide. They could go from:

    {
            MyClass m;
            MyClassInitialize(&m, foo, bar);
            MyClassAddFloatAndInt(&m, 3.0f, 2);
            MyClassDoSomething(&m);
            MyClassCleanup(&m);
    }

    to:

    {
            MyClass m(foo, bar);
            m.add(3.0f, 2);
            m.do_something();
    }

    Even better if they allow the use of C++ namespaces to keep a large project organized.

    The other major win that comes to mind is simply being able to use powerful C++ libraries like the STL. Not having to cook up some kind of container data structure in plain C, but being able to use std::vector<SomeType> and std::map<SomeType, OtherType> and such is a huge win.

    P.S. I read through much of the discussion and here was my favorite post:

    http://gcc.gnu.org/ml/gcc/2010-05/msg00757.html

    steveha

    --
    lf(1): it's like ls(1) but sorts filenames by extension, tersely
  11. Re:Seems odd... by Anonymous Coward · · Score: 5, Funny

    now I'd like to see the graph of what was used to compile each compiler, until the first one written at hand on perforated cards, programmed for abacus in the classical ages

  12. Re:What... by josgeluk · · Score: 5, Funny

    Quis compilabit ipsos compilatores?

    ecco, tibi fixi .

  13. Re:From the article it is obvious by Cyberax · · Score: 4, Informative

    Because ObjectiveC is a slow shit?

    Seriously, it might be OK for designing GUI interfaces, its dynamic nature helps there. But for compiler writing I'd prefer something:
    1) Fast.
    2) Typed.
    3) Deterministic (no non-deterministic GC).

  14. thus a disaster by r00t · · Score: 5, Insightful

    That and the fact that C++ operates on a higher level of abstraction and therefore requires much more careful consideration and planning.

    Planning... so you plan, then write, and you are done? This is a project that is expected to live for decades. The requirements change.

    If you need more planning, that's a bad sign.

    The problems I have seen in the past have all been centered around people not quite understanding the nature of C++ and wanting to immediately put all those bright new features to "good" use, by overloading everything and indiscriminately inheriting from any number of classes.

    Yes. Expect it to happen, despite any efforts to resist. This is the nature of a project with more than one developer.

    The secret to good programming has always been to keep it simple - this is twice as important in C++, and the language has some great features for doing so, but you really have to understand what it is you are trying to achieve.

    Human brains are not SMP hardware. A group of people working together will not all see the same big picture.

    Nobody on Earth fully understands all of C++. Every C++ programmer knows a subset. My subset is not your subset; it is unique to me as yours is to you. Features I love make you uneasy at best, and your pet features do likewise for me.

    The features sneak in here and there... well I just can't resist because I really NEED my favorite feature! Think of the classic 2-circle Venn diagram for two people's C++ knowledge: you might hope for your project to be that intersection in the middle, but it's going to end up with the big fat union of pet features.

    Really, you can't stop it. Resistance is futile.

    You'll see exceptions, then memory leaks, an attempt to solve it with some kind of braindead "smart" pointer, somebody needs multiple inheritance, some ass overloads the comma operator or () operator, overloading gets sort of ambiguous with differences between the 32-bit and 64-bit builds, Boost gets pulled in with compile times and start-up times going to Hell, people cry for Java-style garbage collection...

    1. Re:thus a disaster by MadKeithV · · Score: 5, Informative

      You'll see exceptions, then memory leaks, an attempt to solve it with some kind of braindead "smart" pointer, somebody needs multiple inheritance, some ass overloads the comma operator or () operator, overloading gets sort of ambiguous with differences between the 32-bit and 64-bit builds, Boost gets pulled in with compile times and start-up times going to Hell, people cry for Java-style garbage collection...

      If the first thing you get from C++, coming from C, is exceptions, then you're going to be in a world of pain. Most people who started with C++ have trouble with it. For a quick indication why, see Code Complexity @ GOTW.ca .

      As a 10-year veteran of C++, I say to start with RAII, and since you're going OO, require everyone involved to learn the SOLID principles.

  15. Re:80's technology by Narishma · · Score: 5, Funny

    Yeah, exactly. I don't understand why they didn't chose something modern like Ajax.

    --
    Mada mada dane.
  16. so now I can't grep for functions by r00t · · Score: 5, Insightful

    grep MyClassAddFloatAndInt *.c

    grep add *.c

    This totally sucks. Now I need some complicated language-specific search tool that is sure to have fewer options than grep. It's probably not even scriptable, and surely much slower. Why do you want me to suffer?

    1. Re:so now I can't grep for functions by Joce640k · · Score: 5, Funny

      Ummm... just right click the function name and select "Find all references" from the popup menu.

      --
      No sig today...
  17. Re:Seems odd... by Philip_the_physicist · · Score: 3, Interesting

    If they used an early enough variant of C++, they would be relatively straightforward, since that has already been done once (cfront), but templates would probably make things somewhat harder, and would probably require a decent macro language. If they want some of teh nicer-sounding features of C++0a, things might start getting very horrible quite quickly.

  18. Re:Seems odd... by FlyingBishop · · Score: 5, Funny

    With the nodes that insert a backdoor into the unix login program colored red.

  19. Re:Maybe they've grown up a bit by arth1 · · Score: 3, Insightful

    Why, exactly, is using STL a greater good from a compiler side?
    For a C++ user, sure, but the compiler gains nothing from this, AFAICT.
    It's not like using STL makes code faster or less memory hungry, which, second to the algorithms, should be the focus.

    If I were to guess, allowing C++ serves one real purpose: bringing in the large amount of programmers out there that work with C++. Not because their running code is superior, but because they are there.

  20. Re:Seems odd... by mog007 · · Score: 4, Informative

    At best, the compiler would date back to Grace Hopper, as she was the person who invented the compiler. I believe it was for Fortran.

  21. Re:Seems odd... by Per+Wigren · · Score: 5, Informative

    It was Flow-Matic aka B-0 which later kind of evolved into COBOL, also designed by her.

    --
    My other account has a 3-digit UID.
  22. Re:Maybe they've grown up a bit by Anonymous Coward · · Score: 3, Insightful

    Pride?

    Come on now, let's be fair. They said that boot strapping with only C was the reasoning. If you've ever bootstrapped a compiler or even worked on cross compiling tool chains for a new platform, what they did is huge. There is a reason that GCC is the compiler just about everywhere. you don't have to like it and I'll agree that some of the reuse stuff C++ can provide is potentially huge but it's not pride, it's the desire to be useful.

  23. Re:Maybe they've grown up a bit by ommerson · · Score: 5, Informative

    Quite simply because STL is the embodiment of several decades of algorithms and data structures research work. In many cases, use of STL results in near optimal code. In raw C, you're left to yourself to write your own collections and algorithms. You have to try pretty hard to surpass the performance of STL. Do you want compiler developers to constantly reinventing wheels or actually improving the compiler?

  24. Re:Maybe they've grown up a bit by Eponymous+Coward · · Score: 4, Interesting

    It's not like using STL makes code faster or less memory hungry

    Sometimes it does. For example, compare the stl sort routine with qsort. The stl version is declared with a predicate method that can be made inline. The C version is passed a pointer to a predicate function that can't be inlined. So, the C++ version can eliminate a function call with each compare.

    But this is library and compiler dependent. In theory you really need to know how your compiler and library perform. In practice, it's so mature that everybody is pretty fast these days.

  25. More eyeballs by tepples · · Score: 3, Interesting

    Why, exactly, is using STL a greater good from a compiler side?

    For example, a std::vector may become correct more quickly than a home-grown array list because more eyeballs will be looking at it. In addition, its operator [] can be overloaded to provide bounds checking.

  26. Re:From the article it is obvious by Cyberax · · Score: 4, Insightful

    Nope, not a troll.

    Objective-C is poor. For example, the most useful part of C++ are fast typed template containers.

    Objective-C has only pointer containers which are untyped.

    'Const' support? Nope.

    RAII and smart pointers? Nope. Memory management in Objective-C is quite convoluted, btw.

    So almost nothing useful for general-purpose programming. Except maybe for inheritance.

  27. Re:Maybe they've grown up a bit by tomhudson · · Score: 3, Interesting

    Quite simply because STL is the embodiment of several decades of algorithms and data structures research work. In many cases, use of STL results in near optimal code. In raw C, you're left to yourself to write your own collections and algorithms. You have to try pretty hard to surpass the performance of STL. Do you want compiler developers to constantly reinventing wheels or actually improving the compiler?

    Since this is Troll Tuesday I'll be blunt - that's absolute horsheshit!

    In testing, performance can be 4x SLOWER with the stl than by using c99. Variable-length arrays combined with bsearch make for one of the fastest look-up "containers" going - way faster than any stl algorithm.

    c++ has its uses. It has nice features. The stl, on the other hand, is visually offensive. I bought the hardcover TR1 spec, read it twice, and consider it a waste of time and money. For what most people need, if they can't write their own classes, there's enough generic code that they can modify to achieve their goals.

    On top of which, object-oriented programming is a paradigm, not a language feature. You can do OOP in c just fine by passing an explicit "this" as the first parameter in any function you want to act like a method call. You can even do OOP in assembler - Borland had some nice material on that.

    Use c++ for encapsulation, for references, for the syntactic sugar - but not because the stl will make it run faster - it won't.

  28. Re:Seems odd... by tomhudson · · Score: 3, Insightful

    C++ programmers are the best, most experience programmers around,

    c++ programmers who don't know c all that well (and there are LOTS of them - maybe even the majority) are not the best.

  29. Re:What... by MikeBabcock · · Score: 4, Informative

    Or even from the GCC build instructions:

    For a native build, the default configuration is to perform a 3-stage bootstrap of the compiler when `make' is invoked. This will build the entire GCC system and ensure that it compiles itself correctly. It can be disabled with the --disable-bootstrap parameter to `configure', but bootstrapping is suggested because the compiler will be tested more completely and could also have better performance.

    The bootstrapping process will complete the following steps:

         

    • Build tools necessary to build the compiler.
           
    • Perform a 3-stage bootstrap of the compiler. This includes building three times the target tools for use by the compiler such as binutils (bfd, binutils, gas, gprof, ld, and opcodes) if they have been individually linked or moved into the top level GCC source tree before configuring.
           
    • Perform a comparison test of the stage2 and stage3 compilers.
           
    • Build runtime libraries using the stage3 compiler from the previous step.
           
    --
    - Michael T. Babcock (Yes, I blog)
  30. Re:Maybe they've grown up a bit by XDirtypunkX · · Score: 5, Interesting

    Oddly enough, STL contains a bsearch algorithm that works on variable length arrays and generates code which is pretty damn optimal. It also contains a highly optimized quicksort implementation (along with other sorting and inserting algorithms) that you can use to keep your array sorted. However, even the standard vector operations compile down to pretty much raw pointers if you use iterators, so you can use quicksort/bsearch with no extra penalty on a vector and all the work is done for you.

    So it sounds awfully what you're saying is absolute horse-shit.

  31. Re:Maybe they've grown up a bit by pdusen · · Score: 3, Insightful

    Crappy programmers are crappy whether they lean on the STL or not. Their implementation of pre-existing STL containers and algorithms is bound to be terrible.

  32. Re:Maybe they've grown up a bit by tomhudson · · Score: 3, Interesting

    variable-length arrays were a surprise to me too. I had been looking for the quickest way to do an in-memory search to find keys so I could look up the associated pointer, and as we all know, c doesn't have variable-length arrays ... but c99 does. Use -std=c99 on the compiler line.

    What this means is that you can treat your chunk of ram as contiguous, and realloc to grow it, and if you try to do an array access past the "old" bound, you no longer throw an exception. So your initial array allocation (in my case, 1024 elements) could grown (again, in my case, to a million) and still work.

    Doing a bsearch on that is dirt quick. Being able to refer to any element, even those beyond the original length, using standard array notation, is just SO nice.

    http://publib.boulder.ibm.com/infocenter/comphelp/v8v101/index.jsp?topic=/com.ibm.xlcpp8a.doc/language/ref/variable_length_arrays.htm

    A variable length array, which is a C99 feature, is an array of automatic storage duration whose length is determined at run time.

    If the size of the array is indicated by * instead of an expression, the variable length array is considered to be of unspecified size. Such arrays are considered complete types, but can only be used in declarations of function prototype scope.

    A variable length array and a pointer to a variable length array are considered variably modified types. Declarations of variably modified types must be at either block scope or function prototype scope. Array objects declared with the extern storage class specifier cannot be of variable length array type. Array objects declared with the static storage class specifier can be a pointer to a variable length array, but not an actual variable length array. The identifiers declared with a variably modified type must be ordinary identifiers and therefore cannot be members of structures or unions. A variable length array cannot be initialized.

    A variable length array can be the operand of a sizeof expression. In this case, the operand is evaluated at run time, and the size is neither an integer constant nor a constant expression, even though the size of each instance of a variable array does not change during its lifetime.

    A variable length array can be used in a typedef expression. The typedef name will have only block scope. The length of the array is fixed when the typedef name is defined, not each time it is used.

    Now don't tell me that isn't sweet! For some algorithms, it's a real game-changer, making them so much simpler to implement - and explain to others - that you never want to go back.

  33. Re:Seems odd... by nacturation · · Score: 5, Funny

    The English language, being the whore that it is, pretty much allows you to make any word or phrase mean anything over time, as long as you use the generally accepted meaning at the time.

    For all intensive purposes, I could care less.

    --
    Want to improve your Karma? Instead of "Post Anonymously", try the "Post Humously" option.
  34. Re:Maybe they've grown up a bit by shutdown+-p+now · · Score: 4, Interesting

    I've not only tested it, I've looked at the disassembly of output that various C++ compilers produce from STL code at maximum optimization settings. In pretty much all cases, the algorithms are very aggressively inlined, with the overhead non-existing. I.e. you'd not do any better by manually implementing a sort or a binary search inline at the same point.

    STL containers are somewhat slower when excessive copying is taking place. This was hard to avoid in the past in some cases - you could use std::swap to optimize manually, but it was not always possible. But C++0x has rvalue references and move semantics now to deal with this, and STL containers use them to greatly optimize things. Furthermore, rvalue references enable perfect forwarding, and that, combined with typesafe vararg functions built on template parameter packs allow C++0x STL containers to provide member functions to instantiate objects directly in-place, without any copying (emplace_back etc).

    And g++ already implements all this, so it's immediately applicable here.

  35. Re:Maybe they've grown up a bit by ultranova · · Score: 4, Insightful

    But then, crappy programmers misuse them, not knowing about what is done behind their back, and it becomes slow and bloated code.

    This is a compiler we are talking about. I think that we can assume that people who program the program that turns code into machine code must "know their shit", so to say - otherwise the time taken to compile will be the least of the user's problems.

    Besides, having people copy code from a webpage/programming manual doesn't improve things any.

    Having to specify everything explicitely makes you aware of the complexity/memory usage of what you are doing.

    No, they simply memorize magical mantras that, when regurgitated, will do what they want. It's much better to give such people as high-level libraries as possible and let them use those; the more they have to think about optimization, the more likely they are to do something unbelievably stupid.

    Besides, the exact same argument could be used to condemn first OO, then structural programming, then anything that gets compiled, then finally machine code itself as an abstraction over the physical hardware of modern processors.

    --

    Forget magic. Any technology distinguishable from divine power is insufficiently advanced.

  36. Re:Maybe they've grown up a bit by sim82 · · Score: 3, Interesting

    how true
    I had this funny 'aha' moment doing some simple operations on all elements of an int array: using a stl::vector and an iterator is actually faster than a plain-c int-array indexed by the loop-counter. The stl version boils down to pure pointer arithmetics, while the c version has the indexing overhead. Sure you can do the equivalent in c (as fast as an stl iterator, surprise, surprise), but honestly I like to keep away from this kind of stuff, most of the time.