I'd love to if I had the time to gain enough depth of knowledge of
C++11. But for my professional use of it I try to learn things that work
and are safe and automatically being suspicious of the complex and
clever.
I've always loved C++ whilst always too thinking it really should
never be used for real.
You know something is wrong when most people are using a tool that is
too complex!
Yeah, like most people use a computer. Must be something wrong
then.
"PHP [..] a language full of quirks which at least gives you memory
and type safety". Except, if you look at the code of PHP you'll know
it's *really* not safe. There are so many people sitting on so many PHP
zero-days, it would be hard to count. You just have to run it with
valgrind/llvm-sanitizer and be amazed. It would be hilarious if it
wasn't used by millions, openly exposed to the web.
Perhaps you're right; if so I'd guess that say Facebook's PHP
implementation is safer. The thing is, PHP the language β which in the
long run matters more than the current version of php the standard
implementation β makes a safe implementation possible. A memory-safe C++
implementation is only possible in a completely impractical sense β
namely if pointers aren't implemented as machine addresses. Of many,
many C++ implementations not one ever attempted this over the past
decades and for very good reasons.
So you'll keep getting those zero-days forever if people program
stuff in C++ that is exposed to attackers. Fortunately, programming in
C++ is otherwise so cumbersome and unproductive that few people do such
things.
Hi,
I really feel sorry reading so many defamations towards the C++
language.
Maybe C++ is a little complex, but is is the only native language out
there which is suitable for large projects.
It is no problem to write a image recognition software library in C, or
other system-software, but it is IMPOSSIBLE to write large applications
in C.
Large applications consist of OBJECTS and C++ provides support for OO.
OO modelling of software is way more easy to do than forcing a world of
objects into a structured approach.
Maybe in the future D has the chance to replace C++, this too depends on
the mayor compiler-vendors.
I really wish the author would take himself the time and LEARN the
language and he would not only blame about the fact that he can't read
it.
My dear author: Have patience, learn it, use it and LOVE it ;) instead
of wasting your energy on hating the unknown.
@Stefan: I think Linus Torvalds would like to have a word with you
;)
@Xirdus
If Linus Torvalds wants to have a word with someone, are you his
secretary who can arrange a meeting? Will he explain why the C
developers working on Linux continue to introduce security bugs in every
single release? (An undeniable fact, BTW)
@Stefan: erm... at what point is someone entitled to claim that he
"knows" this shit (which I don't exactly claim but let's say I'm far
from a beginner and I would for instance very much doubt that you can
read C++ better than me)?
Thanks for the article
> they used money, large marketing budgets and large budgets for
creating large standard libraries. A much more honest approach yielding
much better results, I find.
C# and Java are not supposed to be portable to whatever platform
there is; they compile to one bytecode format that runs on one virtual
machine;
here you have much more assumption of where the shit runs on. Right from
the start you have to define a memory model that works with threads, not
as an afterthought in version 11.
By not having to be cross platform you can have higher level and more
specialized libraries β like thread pools; GUI toolkits or cryptographic
libraries. I think that's what he means by 'platforms, not
languages';
In C++ they took the 'lowest common denominator' approach, whatever
that means.
Also Java and C# had the luxury to avoid some of the mistakes of C++,
like multiple inheritance.
Also are you sure that C has been extinguished?
gcc does c99 and is working on full support of c11; I would dare to
say that GCC is bigger than Visual C in this day and age (at least i
would not know how to use the bloated development environment of Visual
C, even if forced at gunpoint)
Yet another point: Dangling pointers to stack locations is something
that can be done in C, this effect can be achieved without much
effort.
Of course C does not have this awful amount of stuff going on
implicitly behind the scene and it does not have template headache;
however in C you can make a awful mess with long wound precompiler
macros (just ask the Linux kernel)
You are right that it C is much more straightforward.
However at this point in time i welcome C++ if the customer pays for it;
though you are right that i probably could have done better with the
effort that it took to learn it...(sigh).
Preprocessor macros, not precompiler macro's. And GCC has its own
extensions that are not portable to anywhere else. Well at least they
took over variadic macros in C99.
Java and C# could have been native languages (as in gcj or say
similarly to D or go which are gc'd but native) and they'd be better in
C++ in all the same ways. Also JVM and .NET run on many platforms and it
makes very little sense to call C++ not-a-platform but these two things
a platform.
Regarding C: a lot of projects want to be portable to VS and hence
avoid anything above C89. Not that C was "extinguished" of course, but
C++ is very widespread in places where C could be a much better fit.
In Polish, we have a saying more or less "there's no point in kicking
eachother with a horse". C++ is this kind of massive beast, but it is
here to stay, together with its mass of fanboys, but also some smart
"consultants" (language lawyers).
It is also true what they say β that C++ is "a federation of
languages". It really covers a large subset of C, which leaves us the
option to stick to "predictable" constructs. There's even std::malloc,
std::realloc and std::free β if you don't listen to C++ fanboys and
televangelists.
Then there's the "older version of Java" β class hierarchies and
polymorphism. You know, all this "a class that has any virtual methods,
needs a virtual destructor" stuff. It's mostly pathetic.
Thirdly, templates. This shouldn't be easily dismissed β the Lisp
guys can now, in C++11, have all their fun from the good ol years, with
variadic templates.
The problem is when you have to port some code from MSVC to G++ and
there are unqualified references to dependent types and names.
Right now, because of template abuse, they came up with constexpr. Of
that I know little about, but it seems like an acceptable variation on
macros and constant folding.
What C++ is unsuitable for, among many many other things, is being an
intermediate language (unless it will be in fact C). My colleague has
written a C++ code generator for our software, and it was and still is a
ton of pain, even for a genius with aspergers like him. We also have
another tool, which takes C++ as input (or pretends to), and this is
actually impossible without being a full blown compiler frontend (which
our tool isn't). One tiny example to show why:
template void f();
const int x = 1;
template <int, x void f() { /* some code to further process */
}
Anyone?
In the end, I try to worry about what C++ does to me. I use it, and
it offers me some possibilities and some pain.
(Digression: C++11 is arguably some improvement over C++03... unless
you still need to support older compilers and cannot use the plethora of
new features, or have to #ifdef them out. Not too much old code goes
broken. With Python 3 β this is not the case. And is is being shoveled
down the throats of innocent newbies.)
I certainly wish, that there will be either someone, or a collective
effort to hoist the FQA standard high, and keep it a source of
up-to-date C++ information. Free from dogmas etc.
The HTML sanitization ate my example. Replace square brackets with
agle brackets:
template [typename T, bool b] void f();
const int x = 1;
template [int, x [ 5] f() { /* ... */ }
// x < 5 β this is the unbalanced "less than sign".
It comes from C++ philosophy of "keyword? no, let's add a new meaning
for an old token." like && for (usually) r-value reference.
Unless its in a function or method template after the bare template
argument (like T&& β then it can also be a "normal" (l-value)
reference. But all readers know this already, right?
C++11 is indeed a better thing to upgrade to than Python 3...
Sigh.
As to C++ β in Russian we have a saying "nothing is eternal under the
moon"...
I posted an explanation, but forgot to assert that I'm a human. My
example got tampered through some HTML sanitization. Imagine square
brackets as angle ones:
template [typename T, bool b] void f();
const int x = 1;
template [] void f[int, x [ 5] () { /* ... */ }
// unbalanced [
I have spent most of my career (short as it has been so far) working
with C++, and the FQA was absolutely invaluable to me. Not only for
pointing out C++'s (many) potholes to swerve around, but also as a
reassurance that no, it's not just me, the language really is that bad.
So, thank you, very much, for creating this resource. I can't say I
agree with everything in there (e.g. I feel that C++ exceptions, while
clearly the worst implementation of the exception feature in any
language that I've seen, are slightly better than useless) but seeing a
critique of C++ from someone that has had to get deep into the darker
corners of the language has resonated with me.
C++ is a language that looks fantastic at first glance, and the deep,
fundamental problems don't rear their head until you're working in the
"second 90%" of the development effort in a large, multi-developer,
multi-hundred-thousand-line project. This is why I now have a
deep-seated distrust of developers who really, genuinely like C++. I
frequently find that they're the sort of developers who switch projects
before the later integration stages, while I generally find myself in
the position of desperately trying to clean up the mess. At a glance,
Stefan up there fits that description. The tragic part is that he's
right that C++ is frequently the only reasonable option for a
development project, simply because there's nothing else really
available. C++11 (which I've been using here for a couple of years)
grinds smooth some of the rougher edges in the language and the standard
library (hey, you can finally create a std::fstream using a filename
from a std::string, whoopee!) but the deep, underlying problems remain;
I never really expected anything different.
Thanks for your message!
I think the two big things that machines can do to fight C++ is (1)
distributed build coupled with incremental linking to make rebuilds
tolerable and (2) dynamic instrumentation tools along the lines of
Valgrind checking for various errors (unfortunately we're very, very far
from a comprehensive dynamic error detection tool for C++). Without
these things large projects become incredibly costly to maintain with
50% or even 90% of the time spent fighting the bloody language.
Unfortunately, on top of that, little can be done to make C++
readable except maybe for draconian coding guidelines β something I for
one am not very good at.
The second Lisp predicate is EQL not EQU and there was a C(indeed not
C++) compiler where pointers were not machine addresses: ZetaC which ran
on the Symbolics Lisp machines.
Thanks for the correction, and as to LispM β let's call it the
exception that proves the point (specifically it proved this point by
going extinct).
>Java and C# could have been native languages (as in gcj or say
similarly to D or go which are gc'd but native) and they'd be better in
C++ in all the same ways
That's because of the preprocessor; with C it gathers one source file
with all included headers into one text, compiles it and type checks
everything against the intermediate representation;
Java and friends compile one file as is and do type checking of what
is known locally in that file, if there is a call to a method of an
object that is not known from given information then that call is not
type checked at this stage;
Once all other files of a package have been compiled then it can perform
type checking of calls from objects declared in the same package; the
object signature lookup is conveniently done from class file β the same
format that is used to run the stuff (but you can do without that like
in D)
So that's another major point where backward compatibility with C is
not quite helpful.
posted too quickly: meant to say that many inconveniences of C++
syntax come from the way it uses the preprocessor.
> Java and friends compile one file as is and do type checking of
what is known locally in that file ... if there is a call to a method of
an object that is not known
Have you used javac before? When compiling a .java file, the compiler
examines every class that is being referenced. If a class has no .class
file or its .java file has a later timestamp than its .class file, then
that .java file is compiled as well β recursively.
(Btw because of this behavior, javac fits Make's
one-file-at-a-time-compilation model very poorly.)
you are a hero :)
Yossi, remember the saying "if you want something done right, do it
yourself."
It's up to you to update the FQA. (Cline's FAQ hasn't changed that
much anyway.)
What else can I say? Your FQA is a great resource. I have it
bookmarked and downloaded. I even used to link to it for fun and giggles
when helping beginners on the cplusplus.com forums.
I don't do that very much nowadays, because I want to get along with
the other members... some of whom openly object to your stated goal of
convincing readers that "C++ shouldn't be used" and call your FQA a
troll site. After all, it's not my business to defend your FQA.
Anyway, I think your FQA is valuable. It is very organic and warm,
and the "oh so it's MY fault" when talking about self-assignment is just
one example of that.
Well... thanks, I guess. "Warm"... I'm sometimes afraid "hot", or
hotheaded... Maybe if I toned it down people wouldn't think of it as a
troll site.
I'm pretty sure in some talk not too long ago Herb Sutter said that
they were aiming, finally, for C99 support in Visual C xx. I thought the
talk was more recent than July 2013, but that's where searches come up
with support for this notion.
From what I gather, the difference is that C++ is "insane" for a
reason (performance/etc) and people who use it generally know this or
they would use something else, while php is bad for no particular
reason.
How does C++'s undecidable grammar help with performance, exactly, to
take one example of very, very many? The real difference is C++ has all
those apologists with Stockholm syndrome while PHP doesn't; also C++'s
creator thinks his creation is perfect and wins people over by
explaining his rationale at length, making them feel like a language
designer through identification with the narrator, while PHP's creator
calls himself a "bad programmer" (and Ritchie called C "quirky and
flawed", etc.) β note that in reality it's a big question which of the
two is the better language implementer.
Maybe C++ doesn't have to be that way, but there's (as yet) no
alternative that absolutely proves it β I mean, most of us have moved on
to Java or C# or beyond, but the C++ advocate can still make the case
that it's higher-performance (or that you can write libraries that can
be called from other languages).
By contrast with PHP we just look at Python or Ruby and see that it
could have been better in every way. (I guess you could argue for PHP's
faster deployment, but if you're that kind of crazy then mod_python does
exist, so it's possible in other languages)
Well, C is better than C++ for high-performance code so there's your
proof... If you want C++ craziness β many people have grown to think of
its problems as requirements β then indeed nothing is better than C++.
But that is just as true for PHP (as in, only PHP has <?php...)
How do you argue that "C is better than C++ for high-performance
code"?
I can think of two things: the executable file is usually bloated;
and there's the overhead of hidden function calls (constructors,
destructors, overloaded operators).
Is there anything else you have in mind, Yossi?
With C you know what's going on (it's not just a question of hidden
functions but also say overload resolution/template
specialization/binding in general), it compiles faster, etc. See http://yosefk.com/c++fqa/defective.html
PHP is ridiculed because its flaws are unacceptable. As a language
mainly developed in the late 1990s and early 2000s, it should not have
most of the serious flaws that it does have. They are unjustifiable
given the state of knowledge when it was developed.
C and C++, on the other hand, pre-date PHP by decades. They were
pioneers in the field. In such a situation, mistakes are expected to be
made, as nobody had experience about what to avoid.
On top of that, we've seen efforts from the C++ community to help
avoid some of the problems that affect it. The faulty aspects may still
be around and supported in order to retain compatibility, but there is
often new functionality that presents compelling and usable alternatives
to faulty functionality. But when it comes to PHP, we've seen very
little done to try to address the problems that it suffers from.
C and C++ have flaws because they pushed at the boundaries of
knowledge, yet at least some effort has been made to mitigate these. PHP
has inexcusable flaws that never should have happened in the first
place, and aren't being fixed today.
Bullshite.
C++'s grammar is a perfect example of something that was known to be
a bad idea when Stroustrup got his CS PhD and yet it's there. PHP is
somehow a tad easier to parse.
"Compelling and usable alternatives"? As in cout vs printf, or STL
algorithms vs loops? Right.
Where "C++ pushed the boundaries of knowledge" I can't even imagine;
most of it was (badly) copied from other languages. Maybe const
represents C++'s contribution to computer science... it's a good thing
nobody was crazy enough to copy it to another language.
And it's not like PHP doesn't have new releases or new "best
practice" advice. In fact I doubt it's worse than the C++ advice where
every decade they tell you to use a new idiom for everything (remember
auto_ptr?)
The only difference between PHP and C++ is the existence of
cheerleaders thinking this shit pushes the boundaries of anything.
Your FQA was enlighting for a undergrad like me. It explained a lot
of things and pushed me to experiment with Ada and understand how
languages work, or are supposed to work
Recently I witnessed another "failure" of C++-related software.
People kept talking about the Eigen matrix library as it was the latest
panacea. It turns out that, predictably, the objects created with that
library do not correctly handle a low-level issue like alignment, which
is best handled by the compiler, and so code using Eigen may or may not
segfault, in different ways. Cool, isn't it?
I'm not interested in C++ at all. In my sector, mathematical
programming software, all I see are simple libraries with ugly
interfaces and lots of unintelligible boilerplate. So much for the
language everyone uses. People who seek alternatives either translate
the library in python, gainging little over expressiveness, or implement
their own little scripting language, which can become a nightmare to
work with. C/C++ have been a real drag for our field.
Anyway, coming back in topic, I read briefly about rvalue references
(people never wonder whether having obscure names and complicated
concepts hinders solving complicated problems rather than helps it). It
turns out, again, predictably, that the newest addition fucks up
template instantiation, type conversion and the like, but can also force
to duplicate code to cater to the new type specifier, as with other C++
constructs.
Eigen... thanks for the tip, will try to stay away from it.
As to rvalue references β indeed it's quite the can of worms, one of
those things in C++11 that I wonder if I'm even going to invest my time
into figuring out.
C++ is and always has been a pointer hell just add subtract and bash
your face in for clean stable code.
Nit: s/doze/dose.
In all seriousness, I don't think too many people are capable of
writing outrageous, funny-cuz-its-true commentary comparable to your FQA
(or even this blog post). Good luck finding a worthy replacement!
The FQA is one of the most accurate and useful descriptions of C++
that has ever been written, and was obviously written by someone, Yossi,
who knows far more about C++ than the vast majority of C++ programmers.
Those claiming otherwise advertise their profound ignorance.
_myarr[5]={0};
I suggest that's not a good example. It clearly (at least to me) is
meant to be an assignment whose LHS is element 5 of _myarr. If the {0}
were an initializer, the LHS would have to be a *declaration*, not just
an expression; it would have to define the element type of the array.
The author of that line of code just made a mistake; I don't think that
particular mistake illustrates a weakness in C++.
Someone who thought an array object could be assigned like that would
probably try to write:
_myarr = {0};
and then realize, when the compiler rejects it, that the
initialization needs to be on the definition of the variable.
Keith Thompson,
All that arcana, simply to declare an effing array?! No other
language even comes close to the stupidity that is C++.
John S. : I'm not sure what "arcana" you're talking about.
To declare an array:
int myarr[5];
To declare an array and initialize all its elements to zero:
int myarr[5] = { 0 };
To assign a value to an element of an array:
myarr[2] = 42;
There are admittedly some oddities in the treatment of arrays (which
C++ inherits from C). The fact that { 0 } can be used as an initializer
but not as an expression is inconvenient. But just declaring the array
is straightforward.
C++ rules. PHP blows chunks
I can't count the number of times I've asked a question on S.O. on a
complex C language issue, and received as an answer "you should move to
C++ (you dinosaur)", typically from a programming student with
approximately zero experience in field code deployment and
maintenance.
It looks to me that programming schools are the main driver that push
C++ on the agenda, since it has become the academic reference, on the
ground that it is "object oriented", so it must be a great thing.
Bollock.
I also deeply regret that C had to suffer from the very existence of
C++, which implicitly pretends to be "C next gen", thus killing any
evolution of C stand alone. C99 is great, but it would deserve more care
to evolve with our time.
C++ is a tool. If it is too complex for your, don't use it.
You could use another language like Java, Python or Javascript and let
your CPU melts.
And if English grammar is too complex for you, don't use it :)
Thing is system level development is almost dead. Game dev, kernels
and maybe compilers depends are about the only reason to look at c/c++.
I love c I hack on gcc and commit to it. But the problem is most work
people do these days are web apps or app development so really there
isn't a need for the speed increase they provide abd higher level
applications are faster abd easier to develop in python or java etc. you
not going to start writing JavaScript front ends to c++ back ends
outside of legacy code projects. The language more over gets abused
terribly in real life mixing c types and idioms into c++ they are two
very different languages but it really does get very very ugly abd hard
to read fast. Although it's much easier to develop mocking unit tests in
c++ than pure c so a lot of the reason to use c now gets harder if you
like test driven development.
Tldr: c++ is abused very easily, readability sucks more features
aren't really solving any real problems. Not enough reasons to use c++
in app development or web stacks.
Thanks again for the effort put in your FQA. Even if you writing
style can hurt someone's feelings I've always found your ARGUMENTS rock
solid. :) I think it's a pity that C++ community does not accept
criticisms. After all (constructive) criticisms (like yours) are the
best way to improve and get better.
For what concerns Stroustrup, I'm not sure that he sees Java/C# as
the ultimate evil.
Citing him from an interview for the 5 most important languages you need
to know, he says "[...] C++, of course; Java; maybe Python for mainline
work... And if you know those, you can't help know sort of a little bit
about Ruby and JavaScript, you can't help knowing C because that's what
fills out the domain and of course C-Sharp. [...]".
So, at least, he recognizes the importance of such languages nowadays.
Not a statement of love at all, anyway.
Your C++ vs PHP comparison is broken. C++'s ridiculous oddities stem
from _consistency_ and robustness of specification. It may have resulted
in a silly language that's insane to write in, but at least it does all
make logical sense (if only from an academic point of view). PHP's
ridiculous oddities are the actual opposite: inconsistencies meted out
in the design for no particular reason.
This wouldn't be relevant even if it weren't patently false, which it
is. Example: C++'s undecidable grammar β a problem PHP doesn't have β
does not follow from any requirement, reasonable or bogus, ever invented
by Stroustrup or any of the many C++ apologists. It's just a goofy
mistake by a CS PhD who should have known better. From an "academic"
point of view this one appears particularly brain-damaged.
You're not the first commenter whom I give this reply, because you're
not the first commenter saying more or less what you said... Why do I
even bother? (And this is why attrition precludes my effective
functioning as the FQA maintainer.)
...See specifically comments #30 and #31.
Just wanted to point out that your example with string comparison is
wrong β in C++ "char*" is not a string, it's a *pointer* and comparing
pointers themselves (not the data that they point to) actually makes
sense, while PHP's behavior in that example is completely
unjustifiable.
You illustrate my point wonderfully. Why isn't PHP's behavior
justifiable, exactly? Many, many languages have many different
comparison operators and some of these do type conversions. (In fact,
nothing prevents you from defining an operator== comparing std::string
and int, PHPifying C++ to some extent.)
As to "what char* is" β well, "abc" definitely looks like a string
but is a char*, not an std::string or any reasonable string type, and
comparing its address with anything is never actually useful, once you
think about it. It's definitely way less useful than having "5"==5
evaluate to true. And while there are historical and other reasons for
it all, the extent to which you perceive this as "justifiable" is
exactly the extent to which you've learned to love C++'s shit instead of
seeing it for what it is...
I'd like to point out that now there's alternative to C++. Rust.
First of all there were always alternatives to C++, just not with the
same feature set as Rust... As to Rust β I have to look into it... It
looks very promising, but also very hairy and without first-hand
experience it's hard for me to tell how it is to program in it. I guess
it's "the only alternative to C++" if you want something high-level
without a gc. There's a reason why most languages have gc, though, and
Rust's complexity in the memory management department basically puts a
spotlight on that reason... Still, you need gc-freedom when you need it,
and I hope Rust proves to be what it promises to be.
I like C++ a lot for some reason. Despite this, I consider the C++
FQA a really nice resource, and the provocative tone makes for a more
entertaining read, and is refreshing in the face of the hype. As far as
I recall, it also is factually correct, so my like and your dislike
comes down to how much the problems objectively disgust us.
I can definitely see how the paternal tone in the old C++ FAQ reads
very provocatively to someone critical of C++.
As for C++11 onwards, I think the additions are really nice (even
though they're burdened by the typical C++ quirks forced by a desire for
compatibility). I think they make programming in C++ much more
productive and when used at the exclusion of some older styles at least
a bit safer.
Of course, the old things don't really go away, so this takes some
studying. I already knew C++ pretty well, so these were "just" additions
to me (admittedly, some still difficult to grasp). Looking back,
considering the amount of time I've spent, the prospect of learning C++
from scratch seems daunting. I don't think I would spend this much time
on a new language today.
* I meant subjective disgust.
Thanks a lot for your feedback!
"In this sense, C++ is fine and a worthy achievement of a lifetime.
Especially in a world where Putin is a candidate for a Nobel Peace prize
and Obama already got one."
This quote just epitomizes the very era we currently live in. Way to
go, Yossi.
It is sad, however understandable, to see the FQA come to a halt, at
least for a time. Specially now that, in the eve of a new "C++ Standard
β’", and after a number of "committee meetings" and several issued
"proposals" (hahhaha!), we're provided with this fresh, truly rich
material for even more intense reveling at the laughing insanity that
C++ and its community is.
Cheers.
Well, it's on GitHub (https://github.com/yosefk/cpp-fqa) so anyone can
edit/update it. Unfortunately, to date, the only change request I got
was replacing "retarded" with "silly", the commit message being
"removing ableist language."
> Well, it's on GitHub
> the only change request I got was replacing "retarded" with
"silly", the commit message being "removing ableist language."
Ironically, accepting this might actually let the project be on
Github for a longer time.
Github went full-SJW lately, actively censoring repositories even for
stuff like this. (one of the examples: https://www.reddit.com/r/KotakuInAction/comments/3eizt4/socjus_github_bans_a_popular_webm_conversion/)
If you think this might be an issue, consider hosting it on https://gitgud.io or https://gitlab.com
I dunno, I don't particularly give a shit one way or the other. If
they kick me out, I'll go elsewhere. I just put it where I thought the
most users are.
> PHP β a language full of quirks which at least gives you memory
and type safety
There is no type safety in PHP. You should look up the definition of
type safety. Comparing C++ and PHP is naΓ―ve β they are intended to solve
a completely different set of problems.
Post a comment