Slashdot Mirror


Bjarne Stroustrup Reveals All On C++

An anonymous reader writes "Bjarne Stroustrup, the creative force behind one of the most widely used and successful programming languages — C++ — is featured in an in-depth 8-page interview where he reveals everything programmers and software engineers should know about C++; its history, what it was intended to do, where it is at now, and of course what all good code-writers should think about when using the language he created."

34 of 371 comments (clear)

  1. yawn by Anonymous Coward · · Score: 4, Insightful

    C++ is a language of a million gotchas. The moment I start having to think about implementation detail and I'm not writing an operating system or compiler, I know I'm using the wrong language.

    1. Re:yawn by gbjbaanb · · Score: 2, Insightful

      then you're going to have a hard time of programming, perhaps you'd be happier being the Boss.

      All languages have "implementation details" and various gotchas. Look on any programming forum for any language and you'll see tips and tricks in using it. I think you're in the wrong job.

    2. Re:yawn by Chemisor · · Score: 3, Insightful

      > C++ is a language of a million gotchas.

      Whenever you want to use a language, you must learn it first. That's true even of Visual Basic. The reason you see those problems of yours as "gotchas" is that you don't understand how the language (and, in the case of C++, the computer) works. If you let the language shape your thoughts instead of trying to cram your crummy thoughts into C++, it would have been much easier and simpler for you.

      > The moment I start having to think about implementation detail, I know I'm using the wrong language.

      In other words, you don't want to know how your program really works. A fine attitude for a PHB. I suggest you switch to english.

    3. Re:yawn by Anonymous Coward · · Score: 2, Insightful

      You evidently either (A) don't know thing one about C++ or (B) FAR FAR FAR worse, you do think you know about it.

      Next time you're in a bookstore, browse through Scott Meyer's "Effective C++" books.

      They're basically a huge list of comments to the effect of, "Gee, you THINK you can do X because it's perfectly legal syntax and it makes sense because you do X in other object-oriented languages, but in C++ it either fails outright or its undefined behavior in the language, so it will fail at the worst possible time in the worst possible way".

      For example, if you declare even one virtual member function, you HAVE to declare your destructor virtual. Ummm, what? Don't I get some help from the compiler?

      This, most emphatically, is not "you let the language shape your thoughts", this is psychosis.

    4. Re:yawn by clickety6 · · Score: 2, Insightful

      >> The moment I start having to think about implementation detail, I know I'm using the wrong language.

      > In other words, you don't want to know how your program really works. A fine attitude for a PHB. I suggest you switch to english.

      When I'm driving my car and I turn the steering wheel right, I expect the car to run right, without having to think about exactly how all the rods and pinions and bearings and whatever are making the car turn right. Sure, I need to know that the more I turn the steering wheel, the tighter I turn, but that should be it. Why should a programming language force me to think about low level implementation details that are nothing to do with the algorithm I'm trying to write?

      --
      ----------------------------------- My Other Sig Is Hilarious -----------------------------------
    5. Re:yawn by iMaple · · Score: 2, Insightful

      When I'm driving my car and I turn the steering wheel right, I expect the car to run right, without having to think about exactly how all the rods and pinions and bearings and whatever are making the car turn right. Sure, I need to know that the more I turn the steering wheel, the tighter I turn, but that should be it. Why should a programming language force me to think about low level implementation details that are nothing to do with the algorithm I'm trying to write? It is nice to know when you are driving the car that if there is snow or an oil slick or a flat your turning response would be different. You don't have to know how the steering works, but know a few things more than just rotate the wheel right are useful when you want to do anything non-trivial. And just like programming you will learn these things by experience (if u don't get into any serious crashes).
    6. Re:yawn by khchung · · Score: 2, Insightful

      Whenever you want to use a language, you must learn it first. That's true even of Visual Basic. The reason you see those problems of yours as "gotchas" is that you don't understand how the language (and, in the case of C++, the computer) works. If you let the language shape your thoughts instead of trying to cram your crummy thoughts into C++, it would have been much easier and simpler for you. I used to think like that when was still a C++ programmer, until one day I read an FAQ somewhere that details the possible differences between "f(a++)" and "f(a); a++;". That's when I saw the light and seriously switched to Java. I think when a normal person looking at a normal piece of code cannot know what it will do, there is something wrong with the programming language.

      Quick question (which I always used to interview candidates claiming familiarity with C++):

      What, if any, are the differences between 2 programs, one using "f(a++)" and the other "f(a); a++;"? For added fun, also compare "f(++a)" and "++a; f(a);".

      Hint: If you answer "no difference" you don't know enough about C++.

      More hint: there are at least 2-3 ways where the result of the program can differ significantly, depending on the signature of f(), the actual type of a, how is operator ++ defined for that type, etc.

      --
      Oliver.
    7. Re:yawn by bigstrat2003 · · Score: 2, Insightful

      Oh, java must be crap because you can't tell, right? It's pretty damned easy to tell what should happen. f(foo(a)); should run f on the return value of foo(a). f(a); foo(a); will depend on the specific functions involved (and whether a is passed by reference or value), so it isn't the language's fault if your example is bad. In the c++ example, the expected behavior is clear: f(a++) should be the same as f(a); a++; because it's a postfix increment. If the actual behavior doesn't match that, that's C++'s fault. Likewise, if the Java behavior doesn't match what's logical, that's Java's fault.

      Also, saying "Java sucks too so that makes C++ not suck" doesn't hold any water. If C++ sucks, and Java sucks too, they both suck, C++ doesn't magically become ok. It's not like C++ sucking depends on Java not sucking.

      --
      "16MB (fuck off, MiB fascists)" - The Mighty Buzzard
    8. Re:yawn by Chemisor · · Score: 2, Insightful

      > When I'm driving my car and I turn the steering wheel right, I expect the car to run right, without having to think

      That's what I would expect when using your software, because that is the proper analogy to driving the car. Programming is more like designing the car, and yes, you do want to know what the steering wheel does. What if car designers just had little modules described as "this thingy makes the car turn right"? Then they would just snap the parts together and secure them with duct tape. Would you drive a car designed in this manner? I thought not.

    9. Re:yawn by Plutonite · · Score: 3, Insightful

      This is the problem with people who don't know how to appreciate C++ capabilities. Do you even know why a "virtual" declaration on a method may be useful, or what it does internally? The whole idea is that you write code that can call methods named in a base class but defined in a derivative (child), via pointers. So, if you want to keep your code clean, you just have one line like:

      Parent *p = new Child();

      and the rest of the "user" code stays the same. You change the one line above to change functionality of every virtual method.

      Now since destructors are called implicitly most of the time, and since you OBVIOUSLY DECLARED VIRTUAL METHODS FOR A REASON, the compiler will warn you if the destructor is not virtual too, because then the object will be destructed as if it is a Parent object. It is a very valid warning, and will save you memory leaks(child objects contain more stuff to be freed..etc). It all makes sense now, see. The compiler is being nice, yes? Do you not agree that you should be blushing, after accusing the heavenly father Stroustrup of psychosis?

      Advice for life in general: If you don't know how to use something, don't use it ;)

    10. Re:yawn by ultranova · · Score: 3, Insightful

      When I'm driving my car and I turn the steering wheel right, I expect the car to run right, without having to think about exactly how all the rods and pinions and bearings and whatever are making the car turn right. Sure, I need to know that the more I turn the steering wheel, the tighter I turn, but that should be it. Why should a programming language force me to think about low level implementation details that are nothing to do with the algorithm I'm trying to write?

      Because a programmer is a car designer, assembler or a mechanic, depending on his specific job description. The user of the program is analogous to the driver of a car.

      If you want to be a car mechanic, you'd better learn how cars work. If you want to be a programmer, you'd better learn how programs work. You'd think this would be bloody obvious, but oh well...

      --

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

  2. Re:Humour by Rob+Kaper · · Score: 3, Insightful

    The pun seems to be that KDE isn't structured, efficient, portable or serious, despite being written in C++. I can't blame you for missing it, or finding it not funny.

  3. Re:Language stability by LizardKing · · Score: 2, Insightful

    Stoustrup probably means the binary still runs on the now antiquated system it was originally compiled for. I very much doubt he means that the 20 year old source code still compiles with a modern compiler, as the language has changed way too much. So, Stoustrup's probably being a little bit disingenuous as usual.

  4. Re:managed code by Anonymous Coward · · Score: 1, Insightful

    unmanaged code is (deservedly) dying out.

    What are you smoking?

    Our shop now mandates only managed code.

    Which means some PHB swallowed Microsoft's .NET sales lines, not that native code is in decline.

    I think any language that doesn't support managed code is going to die out over the next 10 years.

    Think what you want, if that extends to the belief that kernels, device drivers or any serious (non-research) systems work is going to be deployed as memory managed JIT'd bytecode -- you're a fool. Performance optimizations are still inline ASM, put that in your "managed code" pipe and smoke it!

  5. Re:Printer Friendly by Anonymous Coward · · Score: 2, Insightful

    If the messages are the same, no one has any reason to see both, so scoring one out of the default view is the Right Thing. If you're pathetic enough to care about karma and this happens so often that it even matters, complain to the admins that the author of a redundant comment hasn't done anything wrong.

  6. Re:And ... by Anonymous Coward · · Score: 4, Insightful

    ...it's largely a waste of time. The author spends an inordinate amount of time complaining that C++ prefers compile-time overhead to run-time overhead, and has no understanding that C++ is designed to have no unnecessary performance penalty relative to C. It would be nice if he did, as whatever insights the FQA author has concerning OO languages could be gleaned without wading through a few thousand lines of whining over the lack of things like garbage collection, heap compaction, run time bounds-checking, etc. He also has apparently never heard of Boost.

  7. Cfront by metamatic · · Score: 2, Insightful

    And in fact, Cfront was a compiler, not just a preprocessor; it was just a compiler that compiled C++ into C.

    --
    GCHQ Quantum Insert installed. If only our tongues were made of glass, how much more careful we would be when we speak
  8. The FQA is "equally" partisan? by Anonymous+Brave+Guy · · Score: 5, Insightful

    I'm afraid that web site is one of those things that gets way too much attention in some on-line communities because of its controversial nature.

    The reason the two sides are far from equally partisan is that Stroustrup freely admits there is another side to the debate and that C++ has its flaws, and he is making efforts to improve the situation. The FQA, on the other hand, just makes blanket statements like "For example, the lack of garbage collection makes C++ exceptions and operator overloading inherently defective", which simply isn't true (and neither are many of the statements made in the FQA under those particular headings).

    If you read the comments the guy who wrote the FQA makes on forums like reddit, as well as throughout the FQA itself, it's pretty obvious that unlike Stroustrup, he has little interest in any balanced discussion on the subject. He's just out to prove the other side wrong — where "wrong" often means "not agreeing with him" — and perhaps, the cynic in me suspects, to make a reputation for himself in the process.

    --
    If you disagree, post your argument. (-1, Overrated) isn't your personal censorship tool for views you don't like.
  9. Want to know more? Read the book by idiot900 · · Score: 3, Insightful

    "The Design and Evolution of C++" by Stroustrup is a must-read if you are interested in why C++ is the way it is.

    After reading it, I really hated C++. It's the classic example of a project that gets ruined by too many people working on it, all with their own goals, and the book tells you exactly why this happened. C++ now is a hideously complex monstrosity that is popular because it is all things to all people, not because it is a good language.

    Anyway, if you disagree with me, have a look at the book. It is a testament to Stroustrup's objectivity that I came to the conclusion I did, and that you may come to the exact opposite conclusion as me after reading it.

  10. Re:useful but oh so flawed by pclminion · · Score: 2, Insightful

    Back that statement up, buddy.

  11. Re:useful but oh so flawed by hey! · · Score: 5, Insightful

    Well, there's no doubt in my mind that C++ is a language design tour de force. The question is whether its design objectives are the right ones.

    They were probably the right objectives for the place (Bell Labs) and time (1979) it was conceived.

    At the time, computers were inconceivably slow by today's standards. I worked at a small developer that had a very nice AT&T 3B2-400, which had a WE32000 microprocessor, which probably ran at about 10-15MHz; a half dozen programmers shared it.

    As for the place, well, it was crawling with C programmers and C libraries, doing rather complex and important systems programming. Compatibility with C and proven C libraries would have been a huge thing.

    So, an efficient, object oriented version of C was probably exactly what was needed.

    I think that if there was any fault, it was the attempt to meet the goals of efficiency and compatibility with a language that implemented everything that (at the time was thought to be) necessary for programming in an object oriented style. Multiple inheritance carries too much baggage when all you want to do is to guarantee objects have a certain interface. Likewise, I think operator overloading is another example of trying to do too much. Yes, it makes programmer classes "first class citizens", but it really has no demonstrable practical benefit in my opinion. In situations where you need a special purpose language, it's probably better just to create one.

    Still, that's hindsight. If you really understand all the things Stroustrup was trying to do, C++ is quite awe inspiring.

    --
    Post may contain irony: discontinue use if experiencing mood swings, nausea or elevated blood pressure.
  12. Re:The Truth about C++ by lefticus · · Score: 5, Insightful

    Yawn.

    If you don't like C++, you probably just don't understand it. Yes, it's a complex language. However, if you use RAII (a fundamental tenant of C++) you will not. leak. memory. ever. Same arguments about C++ are used over and over again by people who don't grok the language. Is it the end-all be-all language? No. But it is darn good at what it does (performance minded system level code) with almost none of the problems C has (memory leaks and weak typing).

  13. Re:useful but oh so flawed by everphilski · · Score: 3, Insightful

    Likewise, I think operator overloading is another example of trying to do too much. Yes, it makes programmer classes "first class citizens", but it really has no demonstrable practical benefit in my opinion.

    I write math codes for fun and for a living. I have this discussion on an infrequent basis with a Java buddy of mine. Now granted I'm a dumb mechanical engineer and he's a smart CS major, but when I need some custom math classes that aren't provided by the language (tensors, vectors, Jacobians, Quaternions, etc.) and evaluating long math expressions, it is so much easier to view it using the native math symbols than to nest it all in member functions .add(), .subtract(), .multiply(), etc. Once I transliterated a piece of C++ code I had wrote for him and it consumed three times the space and was much less readable due to the nesting of the member functions... being able to use natural math symbols and natural parenthesis makes the math so much more readable, and when 90% of your code is math, you come to appreciate it. I wouldn't have it any other way, at this point.

  14. Re:Use this link to read article on one page by Animats · · Score: 2, Insightful

    Array sizes ARE carried along with arrays.

    No, they're only available where the original declaration is visible. Try:

    void arraylength(int tab[])
    { printf("size of tab: %d\n",sizeof(tab)); }

    which won't work. "sizeof" is not meaningful in that context.

    Compare FORTRAN, which allows

    SUBROUTINE ARRAYLENGTH(TAB, N)
    INT TAB(N) ! conformant array declaration using param
    WRITE(*,*) SIZE(TAB) ! write size of array, as known by language

    This carries along the info needed for subscript checking. It also encourages users to request the size of an array from a known good source.

    The retrofit of conformant arrays into FORTRAN wasn't perfect, but it's ahead of C/C++. ISO Pascal, of course, also has conformant arrays, as do Modula 1/2/3, Ada, and Delphi.

  15. Re:useful but oh so flawed by everphilski · · Score: 3, Insightful

    On the other hand, that's not the only natural way to handle the situation you are talking about. Another way is to create a new language. Altogether, I'd rather do the kind of work you're talking about in Matlab or Octave.

    I write code for clusters, now specifically CFD. Lots of math, lots of parallel processing. Matlab and Octave isn't gonna cut it, and you really desire the close to the metal aspects of c++.

    It may be a narrow range, but there's a lot of people in this narrow range. Specifically, (mechanical/aerospace/etc.) engineers. C++ isn't sexy like Java or Python, but we do a lot of things you just can't do in Python, and can't do fast in Java.

  16. Re:useful but oh so flawed by toriver · · Score: 3, Insightful

    Basically when you have rules you ought to (or "must" unless you want magical bugs) follow that are not enforced by the compiler, the language is flawed. Like when you oveload new but not delete and thus have incompatible memory management. Or you return a reference to a method-local (auto) string object.

    It does however give rise to a market for code analysis tools that checks all the stuff the compiler will let you get away with.

    But you can save the cost of these tools (or the alternative manual hunt for bugs) by using more modern and productive languages like Java or Ruby, leaving C++ for operating systems and games. And the latter is moving into other lanbguages as well, i.e. Microsofts push for C# in game development, and the widespread use of Python in e.g. EVE Online, ToonTown, Civ IV and other games.

  17. Bitmasking by bill_mcgonigle · · Score: 2, Insightful

    Hex is close enough and less error-prone

    When you're actually bitmasking, it's nice to see the bits rather than having to accumulate them in your head.

    --
    My God, it's Full of Source!
    OUTSIDE_IP=$(dig +short my.ip @outsideip.net)
  18. Re:useful but oh so flawed by twistedcubic · · Score: 2, Insightful

    Altogether, I'd rather do the kind of work you're talking about in Matlab or Octave. Or you could do something like Beanshell -- have an interpreted language that is closely tied to the underlying language and its libraries. Matlab (and its clones) are mostly useless in the numerous areas of math outside of a standard undergrad curriculum, even for "profiling" since the data structures aren't even in the language. It's a zillion times easier to write these in C++. This never gets mentioned on forums like this, but C++ provides the right balance of power and flexibility (especially with memory managament) for people who do specialized math/science. And even in not-so-specialized areas, sometimes objects in Matlab clones are represented in a way which is cumbersome to use. Writing a representation in C++ might save hours.
  19. Re:useful but oh so flawed by twistedcubic · · Score: 2, Insightful

    If 90% of your code is math, how about Lisp or Fortran? Maybe Lisp is too slow and it's hard to represent objects in Fortran. Imagine how you would represent a multivariate polynomial with rational coefficients in Fortran.
  20. Re:The Truth about C++ by jamesswift · · Score: 3, Insightful

    It's much harder to write C++ code that, for example, will never leak memory no matter what goes wrong than in the assorted garbage collected languages, or even vanilla C. That, I don't see how anyone could even reasonably argue.

    Probably true. But what does that tell us about general language fitness really since it's equally as easy to hog resources in a language with GC? Database connections for example.

    When you absolutely need deterministic release of resources you end up having to approach the problem in a similar fashion to c++ memory management anyway.

    Many people believe seem to believe GC allows you to forget about resource management when it doesn't at all.
    It's a great tool for a certain class of problems but not a panacea.

    --
    i wish i could stop
  21. Re:C++ Debuggers by Anonymous Coward · · Score: 1, Insightful

    You might be surprised how many smart people think you're better off actually understanding your code instead of having the computer explain what one path happens to do right now.

  22. Re:C++ Debuggers by Traf-O-Data-Hater · · Score: 3, Insightful
    "you're better off actually understanding your code instead of having the computer explain..."

    That's just the point. Your code isn't the problem for you to understand. In the real world where people have to look at other people's code, you often need all the debugging help you can get.

    Stroustrup is being very smug in his response here. He lives in an ivory tower, how much real-world code written by other people (to a deadline or management constraints) has he ever dealt with?

    My feeling is, very little..

  23. Re:useful but oh so flawed by zaphle · · Score: 2, Insightful

    Multiple inheritance carries too much baggage (...) but it really has no demonstrable practical benefit in my opinion. This is basically heresay. Most people who *think* multiple inheritance is not needed have never gotten into programming deep enough to understand that it actually *is* necessary if you want a clean object model. Sure C# and the like give you interfaces, but they require you to implement the code behind them over and over again, resulting in most programmers to just start copy-paste code. Then one day, a bug or change request comes up for that particular copy-pasted code and hell starts: you can start updating all that copy-pasted code and make sure you don't forget any of it. With multiple inheritance, the shared code is actually where it should be: in one class. I dare people to actually show an example of where multiple inheritance is flawed that can't be solved with virtual inheritance or dynamic_cast. The problem is that most programmers who *think* they are C++ programmers are actually enhanced C programmers (at best) who never even heard of dynamic_cast or virtual inheritance, not to mention partial template specialization. Here's a link if you want to know more: http://en.wikipedia.org/wiki/Modern_C%2B%2B_Design

    I also like to remind that generics as now widely used in languages such as Java and C# initially started as templates in C++, only the latter has far better support.

    For those who think that garbage collection is the holy grail against leaks in a program, think again. While it may solve memory leaks, it doesn't solve resource leaks (like open connections, file locks, ...) who on the other hand can be nicely solved in C++ by implementing their release in the destructor. The use of garbage collectors inherently prevents the destructor paradigm (since one can never be sure when or even IF the Finalize method will be called - yes the GC of .NET doesn't guarantee that it will be called even when the program terminates, so freeing up a resource in a Finalize is a no-no since it may still leave you with locked resources), so basically, you're required to manually clean up your resources instead of having your object deal with the cleanup automatically. Makes garbage collection quite controversial if you ask me.

    If you want to know more, I'm always in for a discussion on the subject. Happy to inform.
    --
    And what if there's nothing behind the door until it is being opened?
  24. Re:C++ Debuggers by zaphle · · Score: 2, Insightful

    Just a side-note on unit-tests and debuggers (actually off-topic): They're all fine and well, but bugs resulting from bad synchronization (e.g. use of mutex etc) in multithreaded programs will not be detected. Often, if an object is not well synchronized, the chance of having race conditions or lost updates are generally very small and therefor hard to reproduce, but in a production environment they would still come up fairly regularly. E.g. If there's a chance of 1 in a million for something to go wrong due to bad synchronization, you would only encounter it on average every one millionth time you debug your application (=never), while for a computer it's not a big deal to run a piece of code billions of times per day. By definition, unit tests are never run while other threads are touching the same memory because they are meant to test an isolated unit, which boils down to testing them on a single thread, so unit tests will even never spot them.

    Don't get me wrong, it's good practice to have unit tests, but there are actually people out there (often IT managers) who imagine that if all code is covered by unit tests that this would guarantee that the program as a whole can't fail so I always like to add this disclaimer when talking about unit-tests or testing in general.

    --
    And what if there's nothing behind the door until it is being opened?