Hacker News new | past | comments | ask | show | jobs | submit login
GCC switches from C to C++ (gcc.gnu.org)
142 points by Xyzodiac on Aug 15, 2012 | hide | past | favorite | 109 comments



>>we'd have to get used to using auto_ptr

>auto_ptr is broken. We should use shared_ptr instead

It has begun!

One of the most enjoyable features of C++ is the arguing over which parts of the language should be allowed.


Just to be clear, the replacement for auto_ptr is unique_ptr, not shared_ptr.

The vast majority of allocated objects have unique ownership semantics, not shared. If you use shared_ptr everywhere, it's far too easy for your design to degenerate into object soup.


> If you use shared_ptr everywhere, it's far too easy for your design to degenerate into object soup.

You mean that it's usually better design to have only one "owner" of an object? That's true.

It's worth noting that shared_ptr is also necessary for containers, so you might get into a habit of using it even if you're not creating multiple references.


shared_ptr isn't necessary for containers, it's only necessary for bad containers.

Edit: Of course I'm being a bit stupidly snarky here, obviously if you have a standard container you need a shared_ptr, or an intrusive_ptr, or some other pointer, the moral of the story is that std containers aren't good for holding owned pointers in C++03.

I don't think boost ptr_containers are particularly good either, but that's mostly because of the huge cost of putting the string '#include "boost/' in your code.


Not only. Over-use of shared_ptr is sloppy, and may cause reference leaks which are no easier to solve than memory leaks. Its use is recommended only when sharing semantics are actually required.


No it isn't. unique_ptr works very well in containers.


If the ownership semantics are clear, scoped_ptr works too.


In addition to it being broken, in C++11 auto_ptr is depreciated. Since they're moving to C++ now, they may as well use the latest features as often as possible (when the use case makes sense of course).


No, it's a bad idea to use the latest features. GCC still has to be able to bootstrap. This is why it was still using C90 in the first place.


And everybody knows that ++C is often more efficient than C++


That would be an in-place update to C. It's happened a few times, it's now C11 instead of C99. Sort-ability went out the window because of Y2K.


I avoid both but how is auto_ptr broken?


auto_ptr's copy is broken, so if you copy one the first object loses the reference. Also, it always uses delete on the reference, which means it can't be used for things that don't use delete (like arrays which use delete[]).


Before the days of std::move, auto_ptr's copy modeled an ownership-transfer semantic in a more efficient way than could be accomplished by shared_ptr (no refcount churn). It wasn't broken; one must have simply known when it was appropriate to use.

Consider a helper function that returns a heap-allocated object and think whether auto_ptr or shared_ptr better modeled the function's intent of giving ownership of the object to the caller.


The problem with auto_ptr is that it put these ownership-transfer semantics in the copy constructor instead of something more explicit. A scoped_ptr type that has a release() method is far more useful and safer than auto_ptr.


Sure. I was more responding to the claim that shared_ptr should always be preferred to auto_ptr. I agree that scoped/unique_ptr fixed a lot if auto_ptr's pitfalls.


This was just a workaround for the lack of move constructors.

The real problem with auto_ptr is that if anything emits a call to auto_ptr destructor at the point where the object pointed to is incomplete (forward-declared), the destructor code won't run, only memory would be freed.


Lack of move semantics.


A useful discussion of the conversion and its motivation in the GCC Wiki:

http://gcc.gnu.org/wiki/cxx-conversion


Here's the rationale from that document:

* C++ is a standardized, well known, popular language.

* C++ is nearly a superset of C90 used in GCC.

* The C subset of C++ is just as efficient as C.

* C++ supports cleaner code in several significant cases.

* C++ makes it easier to write and enforce cleaner interfaces.

* C++ never requires uglier code.

* C++ is not a panacea but it is an improvement.

It's interesting to me how defensive many of those justifications are. "It's just as efficient!" or "It's just a superset anyway!" Not until halfway through the list do we get any real description of expected benefits: C++ makes it easier to write and enforce interfaces, and C++ supports cleaner code in several significant cases. The last item is the most telling though: it's clear that these developers feel they've hit a point of crisis, and they are willing to take risks like this in order to lift themselves out of the problem.


> * C++ never requires uglier code.

No... but it sure tends to encourage it.


You've not seen ugly code until you've read 90s-era Win32 UI code written with Hungarian notation everywhere. Ugly code comes from the programmer, not the language.


I blame the early Windows team by mistaking what Simonyi meant with Hungarian notation by misunderstanding what 'type' means. Which is why Office uses his notation in a much cleaner fashion.


Yea it's a shame that Hungarian notation has gotten such a bad rap. Done right i find it occasionally useful. Done the way most windows programmers have been taught is an abomination.


Can you give an example(or a link to an article discussing this)? I've never had the displeasure of working with the Windows API(though I often shook my head when I saw code for it)


Basically he intended 'type' as not 'datatype' as in 'dwSomething' (for double word) but the purpose of the variable. For example, if you're using an integer variable to store the length of some entity, you would name it something like 'lenSomething' as opposed to 'dwSomething'.

Storing the type of the variable makes little sense as in most cases it is only a quick grep away.


In the example you used, "context" seems to be a more appropriate word than "type".



That Wikipedia page does such a better job of describing it than I did.


http://www.joelonsoftware.com/articles/Wrong.html You have to read a bit, hungarian notation doesn't really show up until maybe 4/5 of the way through.


It definitely comes from both. Objective C Block prototypes? C typedefs? blech.


Unless I misunderstand you, block type syntax is almost identical to function pointers. In both cases, you should generally typedef them and then your code is once again free from ugliness.


Having to typedef at all is a sign of deep, unfixable badness.


I am unable to reply to my sibling poster, but I believe what you're saying is that having a hideous and unreadable syntax that forces the use of typedefs just so others can understand what you have written is a sign of deep, unfixable badness. And I agree.


I'm not sure how you arrived at that belief, unless perhaps you think everything should be (visibly to the programmer) type-free? Care to elaborate? I think it would be nice if C and C++ supported more type semantics than `typedef` and `class`, say something as powerful as Haskell's system...


It's the fundamental unfixable crappiness of C declaration syntax that makes 'typedef' necessary.


Example? What language doesn't need typedefs?

Java doesn't have typedefs, and most everyone agrees that's a language bug.


typedef as type aliasing: uncontroversial. typedef to paper over hideous syntax mistakes: disgusting.


Function pointers are also blech looking.


Some say there is no such thing as ugly code, only job security.

Also ugly code is less of an issue of an ugly design as you can look past how the code looks if the design of the program is sound.

But ugly is in the eye of the beholder and in that maybe more people are growing up with C++ and with a later dealing with C as apposed to the other way around. So maybe it is in many ways the mindset of the average age of the programmers around today are more comfortable with C++ as apposed to C and in that could see nicely laid out C code as having ugly bits as its easier for them to do it prettier in C++ and a biased C programmer will see it compeletly from the other perspective.

So its one of those debates were the best move is to get the popcorn as some people see ugly code were others see something nice and some will see C++ as better than they would C and vice versa. Just be glad i'm not involved or I'd be mentioning COBOL as a reality check :).


Why would C++ encourage it? i think its just as easy to make a c of convoluted madness as a c++ one :)


Interesting, but understandable given how much some people dislike C++.


I'm guessing they expected a lot of criticism for this change, which could be why the list is so defensive.


Also interesting to see how the style guide has changed: http://gcc.gnu.org/wiki/CppConventions; http://gcc.gnu.org/codingconventions.html


The "Spelling, terminology and markup" section is a bit painful. Does anyone really keep it printed and stapled to the wall in case they write non-zero rather than nonzero? Seems really petty. I hope the enforcement is done by simply running a regexp or ignored altogether.

Code is where conventions are important, within comments I really wouldn't be upset if someone called Objective-C "Objective C".


That's standard stuff for any organization that makes documents - keep in mind that the primary purpose of the list is for documentation and user-visible messages. The NY Times surely has a similar kind of list. Do you think that would be equally as painful?


Every decent paper has one; the NY Times is no exception. http://www.amazon.com/York-Times-Manual-Style-Usage/dp/08129.... 384 pages.


There are entire books written about those sort style details. Personally I find it comforting in a strange sort of way to know that there are rules out there that I can follow if I wish.


this is apalling. The argumets seem to boil down to "I don't like VEC and how hash-tables are done". Oh and the usual bs about interfaces. The result is that now there is a monstrous dependency (a c++ compiler) in place of something that an undergrad can build for a class (a basic C compiler).

I wonder what rms thinks of this. Too bad he is not calling the shots in gcc anymore.


It would take a smart undergrad a bit of hand-holding to write a C complier in say, less than a year. (If you can write a C compiler, you should probably be in the Masters' program.)


Many CS undergrads have a compilers class or section of a class for a single semester in either the freshman or sophomore year. Compilers for simple languages like C (simple in an objective sense, mind you) are easy once you understand a few basic concepts. Compilers in general are fairly easy, what's hard is optimization. See for instance this division optimization: http://ridiculousfish.com/blog/posts/labor-of-division-episo... That's just one clever trick, imagine going through a similar amount of work for dozens of different tricks.


A nonoptimizing C compiler is pretty easy (a friend of mine wrote one in high school), and that's all you need to bootstrap GCC.

He didn't even finish his parser for C++ though.


Strictly speaking, C++03 can't be parsed, only interpreted (into a typed AST); it is not a context free grammar.


You can parse languages that are not context-free. "Parse" just means that you figure out the structure of the file, the word has nothing to do with the techniques used.


From what I've seen, a Master's degree is not much more than finishing school for people who went to unremarkable undergraduate programs.


"What would Linus do?"


he would call people stupid


He is right most of the time though.


No, not really, he's just loud.


Oh okay. I think I'm going to trust the guy who wrote the linux kernel over...duaneb.


No, he has good points, but doesn't really rely on logic so much as vitriolic humor. For example:

http://forum.gwan.com/index.php?p=/discussion/587/its-bs-bec...

Basically, his entire argument is the slippery slope fallacy based on assumptions that may never occur. Plenty of great software is written in C++, even using STL, that doesn't lead to software that is any more difficult to maintain than the equivalent C program. For one thing, refactoring due to design change is a pain in the ass with or without objects. He also doesn't address the fact that polymorphism is very unwieldy in C. While it's very defensible to NOT use C++, I don't think that saying that "C is the only sane language" is fair. That, or Apple, Google, Microsoft, Oracle, and Facebook are full of people who are barking mad.

However, in an environment like Linux and git where code practices may be less restrictive than a corporate or authoritarian environment, the natural restrictions of C (i.e. lack of easy-to-abuse features) may seem like a feature in itself.


Then why not write a rebuttal and ask for his feedback? It's impossible to anticipate every possible counter to an argument, and it would be prohibitive to list and rebut them all within a single message.

> Plenty of great software is written in C++, even using STL, that doesn't lead to software that is any more difficult to maintain than the equivalent C program.

I don't deny that great software is written in C++, but what is your evidence that C and C++ have the same maintenance burden.


I didn't mean to imply otherwise :) merely stating what it is he'd do.


No, he's just prolific.


Is he actually? He only "has" two projects that anyone cares about to my knowledge.


He meant it in terms of calling people stupid.


Two prolific projects that amount to far more than the vast majority of us of is will ever accomplish.


I suppose "prolific" does not necessary imply breadth or depth.


I believe an operating system typically implies both.


Prolific typically implies many things. I have a deep respect for linux and git, but neither are really prolific.

Anyway, I thought the OP meant he was prolific in his tirades, which is very true.


The way I interpreted the word was more "doing lots of things" and less "doing lots of a few things". Either is legitimate in retrospect.


Well, it has already happened at least once for Git [1]. I seem to remember someone else suggesting to move the Linux Kernel to C++ ending up with a similar rant (someone can feel free to dig that up if they have the time).

[1]: http://thread.gmane.org/gmane.comp.version-control.git/57643 /focus=57918


Who cares?


I'm going to go out on a limb here and say that if he was writing a 'normal' application or even a compiler he'd be tempted to use some subset of C++ or maybe even Java.

As it is, for the kernel you'd be crazy to use something other than C.


I don't know about that, the last two 'normal' applications he wrote (git and subsurface) were in C.


> I'm going to go out on a limb here and say that if he was writing a 'normal' application or even a compiler he'd be tempted to use some subset of C++ or maybe even Java.

And you would be wrong:

http://harmful.cat-v.org/software/c++/linus


Well maybe. We won't and cannot know, because Linus will never be programming websites for a bank or writing games for Windows.

My feeble point is that the kernel (and git) are computer programs for computer people doing computer stuff.

Subsurface I'll give you. Mostly.


It will be interesting to see the OpenBSD project's response to this.


OpenBSD has been using their own fork of GCC since the switch to GPL3. They still use the last version on GPL2, 4.2.1.

Later versions of GCC are available as packages or ports but they aren't included in the main system.


I wouldn't be surprised if the folks at the project abandoned their branch of gcc in order not to be "tainted by association". There is a revulsion to C++ that is close to the core to OpenBSD's philosophy. Perhaps pcc or clang will get more attention.


The people in the OpenBSD project seem pretty happy with their toolchain. pcc was removed from the source tree due to lack of progress. Making a really good c compiler is hard and I don't think they have the people or interest at this time.

I don't think they would switch to clang. LLVM is also c++.


>pcc was removed from the source tree due to lack of progress.

That's a shame. PCC seemed an interesting alternative to gcc, though I do recall PCC's v1.0 "revival" coinciding with an April Fools day several years ago. Perhaps it always was to be taken as a joke.

OpenBSD's simplicity in implementation is laudable. The less moving parts there are the less parts there are to break. It's the same philosophy that keeps me driving my 25-year old pickup.


I am not sure they are happy. They have the C++ compiler written in C that they want, but:

- they have to maintain their own gcc version (the last GPL2 one)

- the licensing for clang and LLVM fits the BSDs better than the licensing for gcc.

If they want to keep the ability to bootstrap from a C compiler, they should consider creating a good C backend (configurable with such things as the size of a long) for LLVM. That way, they could convert any C++ code (including clang and LLVM) to C, and thus bootstrap a C++ compiler on a system that only has a C compiler.

Does anybody see problems with that approach?


But isn't LLVM written in C++, too?


But if GCC is now C++ as well, then it removes what may have been the motivating characteristic to use it, when clang is for many purposes an otherwise better, more freely licensed, compiler.


Thanks, I did not know that.


Why? They use older version (under GPL2) anyway: 4.2.1 and 2.95.3.


build change to require a C++ compiler is a no go.

One of the reasons to install gcc on a machine is the absence of a descent C++ compiler. 20 years ago, the C compiler of Sun was completely broken and we were happy to have gcc.


In my experience, a modern version of gcc is extremely difficult to bootstrap on anything but a fairly modern platform (e.g., I was having a hell of a time getting it going on HP-UX 11.23 with a ~2006 compiler). That's not a criticism of gcc. I'm only suggesting that if your platform lacks a modern (read: C++03-compliant) C++ compiler, you were probably already out of luck trying to get 4.7 bootstrapped.


Hmm if you statically link everything gcc needs would cross-compiling from another computer not feasible?


Feasible? Yes. I've cross-compiled GCC a few times in the past year or so.

But it sure as hell isn't pleasant. ;)


It's not 20 years ago.

Nowadays pretty much everyone has access to a Windows or Linux PC, or an OS X Mac, and so has ready access to good compilers. The way you'd handle that Sun nowadays is to build a cross compiler for Sun on your Windows or Linux PC or your Mac, and then use that to build the compiler for your Sun.


The default compilers are nowhere near as bad, but even then, if you're not running a binary gcc, you're just asking for trouble.

It's pretty hard to find a platform for which you can't find a pre-compiled gcc. This was not always the case.


You could still use the older GCC?

Sun's CC -> Old GCC -> New GCC


Converting the stage 1 bootstrapping compiler to C++ is a bad idea IMO. There any many embedded platforms without a C++ compiler (except perhaps downrev gcc).


I wonder if you have tried building gcc recently?

While it doesn't require a C++ compiler, in the last few years it has required an increasing list of fairly modern libraries. I don't think requiring a C++ compiler will make things that much harder.


Why are you bootstrapping on an embedded platform?


so cross compilation is the only option for these embedded platforms?


Don't you generally cross compile on embedded platforms anyway?


If you don't cross-compile, I would say it's not an embedded platform.


So bootstrap the build with gcc 4.7.


The demise of gcc has begun. Not because I don't particularly like C++ but because the folks actually have an interest to gradually start moving a C codebase into C++ as if they didn't have better things to do. Sure, some syntactic things will look cleaner (let's just exclude those that will look uglier for the sake of fairness)--in five years or whenever their transition could be considered complete.


The GCC folks are feeling the pressure from Clang/LLVM, which is a good thing. Under the guidance of the FSF, GCC has intentionally tangled the front end with the back end. The purpose of this was to make it hard to modularize GCC, because if GCC were modular, then you could write a proprietary component for it (like you can write proprietary Linux modules).

Their goal here is to make it easier for new developers to write their own passes or front ends or whatever. GCC may be more mature, but Clang/LLVM are way easier to dive into.


> Under the guidance of the FSF, GCC has intentionally tangled the front end with the back end. The purpose of this was to make it hard to modularize GCC, because if GCC were modular, then you could write a proprietary component for it (like you can write proprietary Linux modules).

Surely you must be joking... do you have a reference for this? Sure, I sometimes the motives of the FSF but this would be quite outrageous from a software engineering perspective.



Just out of curiosity, couldn't the FSF release GCC under the AGPL or something like that and then modularize it all they want? That was my first thought when reading that message. That way they can promote freedom and still have modular software.

Of course, the AGPL might not have existed in 2000, but I haven't heard about GCC moving to the AGPL since then either.


It's actually the opposite; GCC is being ported to C++ because there is actually so much interest in continuing to hack on it. The current codebase is being hobbled in various ways by being limited to pure C. GCC even has a (nasty) internal garbage collector, despite being a C program: http://gcc.gnu.org/wiki/Memory_management

If GCC was dying as you said, nobody would be interested in such large refactors that have no point other than to make development easier.


I would regard attempts to more aggressively refactor a 30-year-old codebase as a sign of health, rather than the opposite. A sick project gets only hacks to overcome the fire du jour.


Wow, I honestly never thought I'd see the day.




Guidelines | FAQ | Lists | API | Security | Legal | Apply to YC | Contact

Search: