News for hackers. Stuff that matters.
Re:Why should I have written ZeroMQ in C, not C++ (2012) by rando3826 on Wednesday May 20, 2015 @08:31PM (#9580306)
And so I will continue to avoid C++ because I'm not a master and will not avoid the many pitfals, and I want to get things done, not become a master of C++.
Re:Why should I have written ZeroMQ in C, not C++ (2012) by vitaut on Wednesday May 20, 2015 @10:00PM (#9580564)
Actually avoiding C++ pitfalls is not that difficult as most of them come from C: manual memory management, macros, varargs, unsafe casts. In fact C++ provides safe alternatives to many of the unsafe C constructs, so if you stick to them you are fine.
Re:Why should I have written ZeroMQ in C, not C++ (2012) by AlexandrB on Thursday May 21, 2015 @01:10AM (#9581053)
C++ not only fails to hide the above-mentioned C pitfalls but adds entire classes of pitfalls of its own. Just a few examples:

* undecidable grammar: http://yosefk.com/c++fqa/defective.html#defect-2

* const inside containers: http://yosefk.com/c++fqa/const.html#fqa-18.1

* template error messages http://yosefk.com/c++fqa/templates.html#fqa-35.17

C is often nicer than C++ because it's simpler: it only has the C pitfalls not the combination of C & C++ pitfalls that plague C++.

Re:Why should I have written ZeroMQ in C, not C++ (2012) by vitaut on Thursday May 21, 2015 @01:39AM (#9581132)
These are trivial issues compared to major safety and usability flaws inherited from C that I mentioned. BTW the C grammar is not context-free either. The compilation speed is mostly due to header system which is again a C heritage, not because the C++ grammar is undecidable. And template error messages are much better now in modern compilers such as Clang. As for the const, its semantics is the same as in C.
Re:Why should I have written ZeroMQ in C, not C++ (2012) by the_why_of_y on Thursday May 21, 2015 @07:21PM (#9585940)
The super-slow compilation speed is mostly because C++ encourages massive all-inline template libraries like the standard C++ library and boost, which need to be parsed and have the used templates instantiated for every compilation unit.

Here's idiomatic hello world, with <iostream> vs. <stdio.h>

> g++ -E hello.cc | wc -l

17906

> gcc -E hello.c | wc -l

842

Re:Why should I have written ZeroMQ in C, not C++ (2012) by jlarocco on Thursday May 21, 2015 @02:34AM (#9581255)
> Actually avoiding C++ pitfalls is not that difficult as most of them come from C

If you really believe that, then you're probably writing dangerous code and not even realizing it.

Most C++ pitfalls come from subtle interactions between features that don't even exist in C, like exceptions, move semantics, templates, threading, constructors/destructors, references, operator overloading, etc. Those are the things people are talking about when they mention the pitfalls of C++.

The things you mention are the pitfalls of C, which technically still exist in C++, but they're not as interesting there because the C++ pitfalls are so subtle and dangerous.

Re:Why should I have written ZeroMQ in C, not C++ (2012) by vitaut on Thursday May 21, 2015 @10:56AM (#9583113)
I don't always write dangerous code, but when I do I fully realize it =). And it has nothing to do with any of the features that you listed.

In fact "threading" in C++ is safer because of the availability of high-quality libraries providing task-based parallelism such as TBB, move semantics is used all the time in C albeit manually with pointer manipulation. It is true that not all of the features play well together, but it's nothing compared to the C pitfalls that I mentioned.

If you want to avoid dangerous interactions, don't overengineer your code, stick to the features you understand. This is true not only for C++, but for any language.

Re:Why should I have written ZeroMQ in C, not C++ (2012) by jlarocco on Thursday May 21, 2015 @12:25PM (#9583727)
> In fact "threading" in C++ is safer because of the availability of high-quality libraries providing task-based parallelism such as TBB, move semantics is used all the time in C albeit manually with pointer manipulation. It is true that not all of the features play well together, but it's nothing compared to the C pitfalls that I mentioned.

Well, if I use libraries to avoid C's pitfalls, that language will be safe, too.

> If you want to avoid dangerous interactions, don't overengineer your code, stick to the features you understand. This is true not only for C++, but for any language.

That's almost useless advice. You might as well say the key to using C++ is to use Python or C instead.

"Stick to the features you understand," doesn't even really work when coding by yourself. C++ is so incredibly complicated there's a very good chance most people wouldn't even really understand the limited subset they've stuck to. C++ devs with years of experience routinely mess up stuff like exactly when copy and move constructors are called.

For example, how many copy constructors get called in this code:

    const std::string myString{"omg"};
    std::cout << myString << (myString + myString);
Most people will have to think about it for a little bit, and a lot of them will answer wrong.

And that advice completely goes out the window when working with code written by other people. And it also means not using any C++ libraries because a lot of them use the full features of C++ and expect (or require) the code calling them use those features, too.

The key to using C++ is to be aware there are many pitfalls, know when you're working with features that are particularly dangerous, have some resources that can help identify and avoid them (like the Effective C++ books), and whenever possible have other people review your code.

Re:Why should I have written ZeroMQ in C, not C++ (2012) by vitaut on Thursday May 21, 2015 @01:03PM (#9583963)
> Well, if I use libraries to avoid C's pitfalls, that language will be safe, too.

I'm not talking about possibilities, but about libraries that are actually available and widely used.

> You might as well say the key to using C++ is to use Python or C instead.

And that might not be a bad idea. If you don't need the power of C++ or don't want to invest time in learning it, then you are better off using a simpler language such as Python, but probably not C for the reasons I pointed out earlier.

C++ is a complicated language, so I agree with your advice of using good learning resources and code review. I don't agree that the issues you mentioned are of the same level as the ones I was talking about. Missing a copy constructor is just not as big of a problem as, say, getting a segfault caused by misuse of varargs.

Re:Why should I have written ZeroMQ in C, not C++ (2012) by jlarocco on Thursday May 21, 2015 @03:12PM (#9584797)
> I'm not talking about possibilities, but about libraries that are actually available and widely used.

Then your original statement makes no sense. The C pitfalls you listed are easily avoided using C++ builtins like std::shared_ptr, templates, the STL, and the rest of the C++ standard library.

> I don't agree that the issues you mentioned are of the same level as the ones I was talking about. Missing a copy constructor is just not as big of a problem as, say, getting a segfault caused by misuse of varargs.

And I'd have to strongly disagree. Some of the pitfalls in C++ are worse than crashing because they can (potentially) silently corrupt data. Pass around pointers or references to objects in an inheritance hierarchy without paying attention, and it's not hard to get memory "slicing" and data corruption. Not understanding the intricacies of std::atomic can cause insanely hard to reproduce deadlocks. And I could go on all day.

At the end of the day, I just think it's flat out wrong to say that most of the pitfalls in C++ come from C and are easily avoided. The C pitfalls are easy enough to avoid, but the new C++ specific ones are just as bad, and usually more difficult to understand and spot in real code.

Re:Why should I have written ZeroMQ in C, not C++ (2012) by vitaut on Thursday May 21, 2015 @03:27PM (#9584903)
> The C pitfalls you listed are easily avoided using C++ builtins like std::shared_ptr, templates, the STL, and the rest of the C++ standard library.

That was exactly my point.

> And I'd have to strongly disagree.

OK, let's agree to disagree.

Re:Why should I have written ZeroMQ in C, not C++ (2012) by cubbimew on Friday May 22, 2015 @10:16PM (#9591760)
The number of copy/move constructors is zero, by the way, it's not hard (well, could be one if you're using 1980s compilers)
Re:Why should I have written ZeroMQ in C, not C++ (2012) by nulltype on Wednesday May 20, 2015 @11:35PM (#9580822)
The pitfalls of C++ that I've seen are mostly the things people build with the safe alternatives.
Re:Why should I have written ZeroMQ in C, not C++ (2012) by vitaut on Thursday May 21, 2015 @01:19AM (#9581074)
Could you give an example?
Re:Why should I have written ZeroMQ in C, not C++ (2012) by nulltype on Thursday May 21, 2015 @04:00AM (#9581494)
I've seen some pretty impossible to comprehend or debug uses of templates. Perhaps my favorite, which represents most of the pitfalls you can create with C++ is the Boost library. And of that, the shining star is this page: http://www.boost.org/doc/libs/1_57_0/libs/geometry/doc/html/...
Re:Why should I have written ZeroMQ in C, not C++ (2012) by vitaut on Thursday May 21, 2015 @09:38AM (#9582641)
I wouldn't call it a pitfall. Boost is written by C++ experts who like to push the boundaries of what can be done with the language. This doesn't mean that you have to debug their code or overuse templates yourself and there is nothing in the language that encourage this. For common cases such as in the standard library, templates are pretty easy to use and trivial to debug with modern compilers.
Re:Why should I have written ZeroMQ in C, not C++ (2012) by GFK_of_xmaspast on Thursday May 21, 2015 @10:26AM (#9582911)
Given the design constraints:

""" A generic library should be able to calculate the distance: for any point class or struct, not on just this mypoint type in more than two dimensions for other coordinate systems, e.g. over the earth or on a sphere between a point and a line or between other geometry combinations in higher precision than double avoiding the square root: often we don't want to do that because it is a relatively expensive function, and for comparing distances it is not necessary """

I suspect a little bit of complexity is necessary. (And can you even build something like this in a more fashionable language?)

Re:Why should I have written ZeroMQ in C, not C++ (2012) by a3n on Wednesday May 20, 2015 @08:39PM (#9580336)
What a strange language, that so much sage wisdom involves what parts of it you shouldn't use. (Former C++ programmer, a long time ago, before the "don't use these parts" advice became a thing.)
Re:Why should I have written ZeroMQ in C, not C++ (2012) by derefr on Wednesday May 20, 2015 @08:52PM (#9580371)
There was a post here recently, explaining type systems are effectively about restricting you from encoding nonsensical constructions. C is ASM with the ability to encode some nonsensical ASM instruction-sequences removed, etc.

The weird thing about C++—and the reason it didn't just absorb/replace C—is that it gives you the ability to encode strictly more nonsensical things than C does. In that sense, C looks more like the descendant of C++ than the other way around!

Re:Why should I have written ZeroMQ in C, not C++ (2012) by pjmlp on Thursday May 21, 2015 @02:19AM (#9581218)
> The weird thing about C++—and the reason it didn't just absorb/replace C—is that it gives you the ability to encode strictly more nonsensical things than C does. In that sense, C looks more like the descendant of C++ than the other way around!

Yet the most famous current C compilers are all written in C++.

If it wasn't the rise of open source and its community attachment to C, mainly in GNU and BSD circles, C would have been long replaced except for the embedded space.

Re:Why should I have written ZeroMQ in C, not C++ (2012) by gridspy on Wednesday May 20, 2015 @08:52PM (#9580375)
Yes, it shares that 'feature' in common with JavaScript.
Re:Why should I have written ZeroMQ in C, not C++ (2012) by bajsejohannes on Wednesday May 20, 2015 @08:58PM (#9580394)
No, actually, it doesn't. C++ is a huge language, javascript is a relativly small one. I know it's funny to compare the size of "Javascript the good parts" with "Javascript the definitive guide", but that's can be attributed to writing style more than percentage of language covered.
Re:Why should I have written ZeroMQ in C, not C++ (2012) by MaulingMonkey on Wednesday May 20, 2015 @11:01PM (#9580737)
> No, actually, it doesn't.

Perhaps less in what features to avoid, but there's plenty of hidden gotchas to avoid in JavaScript still.

https://www.youtube.com/watch?v=et8xNAc2ic8 https://dorey.github.io/JavaScript-Equality-Table/

> C++ is a huge language, javascript is a relativly small one.

C++'s ISO/IEC 14882:2003 weighs in at 786 pages, compared to ECMA-262 (5.1 Edition)'s 258 pages (measuring by PDF reader). A ~3x difference, but not quite the order of magnitude I'd expect from huge vs small.

Re:Why should I have written ZeroMQ in C, not C++ (2012) by furyofantares on Wednesday May 20, 2015 @11:33PM (#9580814)
And we share 50% of our DNA with a tree...

That's not meant to be snarky, but what's is the minimum size of a language spec? I don't know the answer, but I'd think the delta over that would be the better comparison. For example if you can't really spec a language in less than 200 pages then C++'s spec is more like 10 times as big as JavaScript.

Re:Why should I have written ZeroMQ in C, not C++ (2012) by kibwen on Thursday May 21, 2015 @12:33AM (#9580977)
"Number of pages in specification" is a rather useless metric to use to compare languages, since there are so many variables. And not just things like point size or margins or what have you, but the contents themselves vary. Some specifications include an EBNF grammar, others may try to describe it via natural-language rules, and others may just say "see the reference implementation". Some specifications include documentation on the language's standard library (the scope of which is a factor that itself varies greatly from language to language), and some of these will go into differing amounts of detail (e.g. providing upper bounds for computational complexity of algorithms). Some languages will go into great detail about memory models (especially low-level ones), while others will wave it away as an implementation detail.

TL;DR: don't trust anyone who tries to use the length of a specification as a comparable proxy for complexity. I can write a 10,000-page standard for Scheme if you hire me to.

Re:Why should I have written ZeroMQ in C, not C++ (2012) by MaulingMonkey on Thursday May 21, 2015 @05:06AM (#9581641)
> "Number of pages in specification" is a rather useless metric to use to compare languages, since there are so many variables.

How would you prefer to compare language sizes? Gut feel? Surely that's even worse.

I'll certainly admit that - like LOC measurements - it's a metric one can't take too seriously on it's own. On the other hand, it's a useful starting point for discussing said variables.

For example, when I was eyeballing the Javascript standard (I've already read the C++ standard a decent bit), I saw it (like C++'s) defines a grammar, and includes some core library bits (e.g. the Object type.) On the other hand, I didn't see the full browser DOM API you'd use when writing most Javascript - but then again, you don't see any modern graphics or UI widget APIs in the C++ standard either.

This was enough to convince me that they were comparable enough for an orders-of-magnitude comparison, at least. My gut reaction was that C++'s standard might be more detailed - but that'd be biased in my favor, further undermining the argument that Javascript is significantly smaller than C++.

> I can write a 10,000-page standard for Scheme if you hire me to.

But can you find a 10,000 page standard, for any language? Eventually everyone hits a limit to what they consider worthwhile to write, short of perverse incentives.

Re:Why should I have written ZeroMQ in C, not C++ (2012) by kibwen on Saturday May 23, 2015 @09:33PM (#9595032)

  > How would you prefer to compare language sizes? Gut 
  > feel? Surely that's even worse.
Why is it necessary to compare "language sizes" at all? Nobody has ever demonstrated that the "size" of a language (by any definition) has any measurable effect on the quality of the software written in that language. It's as pointless as the classic vim vs. emacs argument.
Re:Why should I have written ZeroMQ in C, not C++ (2012) by MaulingMonkey on Monday May 25, 2015 @01:55AM (#9598835)
> Why is it necessary to compare "language sizes" at all?

It seems relevant in the context of how difficult it is to master a given language, which spawned this thread of debate. Do you disagree that language size is any sort of barrier?

Again, only one metric, to be taken with a measure of salt. But would you prefer we compare languages based on gut feel? Or is comparing languages unnecessary as well?

> It's as pointless as the classic vim vs. emacs argument.

Understanding the merits of vim vs. emacs informs you which you might prefer. The answer to that argument, over which is "ultimately better" is certainly pointless. You can simply choose whichever works best for you, especially as you can change your mind mostly at will.

But people don't simply have the choice of "choose whichever works best for you" when it comes to programming languages on a team project. Nor are languages as fungible as editors when it comes to changing your mind - switching languages mid-project is a massive time sink, and frequently fatal to the project.

Re:Why should I have written ZeroMQ in C, not C++ (2012) by bjterry on Wednesday May 20, 2015 @11:53PM (#9580872)
The scheme R5RS standard is 50 pages, and is considered quite small for any real language. The Haskell 2010 Language Report is 239 pages. The Forth ANSI standard is 220 pages and is a seemingly simple language.
Re:Why should I have written ZeroMQ in C, not C++ (2012) by bremac on Wednesday May 20, 2015 @11:47PM (#9580853)
Scheme has the smallest (useful) language specs that I know of. For example, R5RS is 48 pages (44 if you exclude the back matter.)
Re:Why should I have written ZeroMQ in C, not C++ (2012) by MaulingMonkey on Thursday May 21, 2015 @04:46AM (#9581602)
I imagine Brainfuck could be reasonably specced in around 5-10 pages, based on the size of the Wikipedia description.

EDIT: Said Wikipedia page was sufficiently detailed for me to implement a Brainfuck -> MSIL compiler without additional external references, which I was able to verify worked with a Mandelbrot renderer.

Re:Why should I have written ZeroMQ in C, not C++ (2012) by waps on Thursday May 21, 2015 @12:32AM (#9580974)
Here is one of the smallest useful (ie. turing complete) language's full specification :

> Subtract and branch if not equal to zero[edit] > The SBNZ a,b,c,d instruction ("Subtract and Branch if Not equal to Zero") subtracts the contents at address a from the contents at address b, stores the result at address c, and then, if the result is not 0, transfers control to address d (if the result is equal zero, execution proceeds to the next instruction in sequence).

This is all of it.

Re:Why should I have written ZeroMQ in C, not C++ (2012) by pjmlp on Thursday May 21, 2015 @02:26AM (#9581236)
> C++'s ISO/IEC 14882:2003 weighs in at 786 pages,

Including the standard library.

I don't know about JavaScript, but at least Python, Java and C# are actually quite more when including the standard library.

Re:Why should I have written ZeroMQ in C, not C++ (2012) by bkjelden on Wednesday May 20, 2015 @11:52PM (#9580864)
The big thing javascript has going for it are the browser's dev tools, though.

I've done both and hunting things down in chrome's dev tools is orders of magnitude easier than figuring out what is going on in your c++.

Re:Why should I have written ZeroMQ in C, not C++ (2012) by vitaut on Thursday May 21, 2015 @01:54AM (#9581174)
With great power comes great responsibility.
Re:Why should I have written ZeroMQ in C, not C++ (2012) by dragontamer on Wednesday May 20, 2015 @09:11PM (#9580440)
Good luck finding a language out there that doesn't have pitfalls.
Re:Why should I have written ZeroMQ in C, not C++ (2012) by moonchrome on Wednesday May 20, 2015 @09:31PM (#9580493)
To be fair I think most languages don't have pitfalls as defaults in the C++ sense :D
Re:Why should I have written ZeroMQ in C, not C++ (2012) by dragontamer on Thursday May 21, 2015 @12:12AM (#9580921)
But most languages that have seen prominent use have obscure corners that no one should ever visit.

PHP has absolutely awful parts for example. (Magic Quotes anyone?) Even relatively "clean" languages like Java or C# can be completely mucked around if you abuse reflection or do dumb things (ie: have property getters / setters in C# do something insane... like get {return blah++;} )

I mean, any language that has mutexes (ie: all of them) can more or less lead to absolutely dangerous situations. C++ RAII may not be the best (compared to D or Rust or whatever), but C++ is the most widespread language that has complete RAII support.

If you even learn the most basic of C++ patterns: shared_ptr<>, RAII destructors... (which are useful concepts in C++'s "replacements" anyway), C++ becomes not only a manageable language... but one of the most widespread useful languages current right now.

Re:Why should I have written ZeroMQ in C, not C++ (2012) by Daishiman on Wednesday May 20, 2015 @10:45PM (#9580668)
The amount of things in C++ to avoid practically constitute a language by itself.
Re:Why should I have written ZeroMQ in C, not C++ (2012) by moonchrome on Wednesday May 20, 2015 @11:57PM (#9580879)
Another amusing part is that it's a moving target - some parts of the language actually got better with implementations and spec improvements - so there's a large number of people avoiding useful features on outdated practices (they learned "don't use X" without the reasoning behind it :) )
Re:Why should I have written ZeroMQ in C, not C++ (2012) by Shorel on Wednesday May 20, 2015 @11:33PM (#9580812)
C with classes is undervalued, and templates are overvalued, IMO.

Just switching compilers makes the C code more robust.

Re:Why should I have written ZeroMQ in C, not C++ (2012) by eropple on Thursday May 21, 2015 @12:35AM (#9580982)
Templates can be abused, but there's real value to simple ones such as container classes and even shared_ptr and unique_ptr. The subset of C++ I personally use is pretty small, but I hope I never go back to manual memory babysitting.
Re:Why should I have written ZeroMQ in C, not C++ (2012) by waps on Thursday May 21, 2015 @12:36AM (#9580986)
Templates are the main thing in C++ that you can easily use everywhere with zero costs in compatibility.

And they certainly beat C's "equivalent", macros.

Re:Why should I have written ZeroMQ in C, not C++ (2012) by Shorel on Thursday May 21, 2015 @06:04AM (#9581771)
You can use macros with C++

That's what wxWidgets did since forever, and to the dismay of C++ purists, I find their code easier to read than most templates.

Not to mention it compiles much faster.

Re:Why should I have written ZeroMQ in C, not C++ (2012) by pjmlp on Thursday May 21, 2015 @02:20AM (#9581220)
There is hardly any C feature that beats C++ one's. :)