7 ms·
This is perhaps one of the more unexpected opinions on C++ I've encountered. When I picked up C++ with Turbo C++ 3.0 in the early '90s, it was already widely re
by spoondan 10y ago
This is perhaps one of the more unexpected opinions on C++ I've encountered. When I picked up C++ with Turbo C++ 3.0 in the early '90s, it was already widely regarded as an over-complex behemoth. Donald Knuth said of it in 1993, "Whenever the C++ language designers had two competing ideas as to how they should solve some problem, they said 'OK, we'll do them both'. So the language is too baroque for my taste." The last twenty years, there's no C++ that I would call lean, expressive, and elegant. You give the example of template meta-programming as something added more recently, but Erwin Unruh was printing the primes in compiler error messages not long after Knuth was knocking C++ as baroque. It makes me wonder if perhaps the issue isn't simply that over time you've become more aware of how complex the language has always been?
The language has continued to grow, but it's growing in a rather different direction (or rather in a clear direction): towards a modern, preferred subset that _is_ lean, expressive, and elegant. While C++ is still far from my favorite language, I find C++ today to be far simpler, more expressive, and more elegant than at any other point in the past.
- cmrdporcupine 10y agoArguably the std::move stuff and the semantics around it have added uncomfortable complexity to the language, even if it might have been necessary. Other than that I think other advances in C++11 and beyond have made for a quite nicer language to work in. I love C++11 and use it every day, but I think putting references in the language was an 'original sin'. They should have stuck with C's model: it's either a value or a pointer to a value. References just added a pile of complexity and pontificating about whether something was being copied, moved, or passed by reference.
- JoeAltmaier 10y agoSurely references have intrinsic value to the programmer? They're a pointer that can't be moved or rebased. Other languages have only references, and in C++ you can use that convention and not have any extra confusion.
- makecheck 10y agoExcept that references still can be bad: invalidated later on by an object deletion, no matter how many rules were enforced to assign the reference to begin with. And in an almost-worse sense, "sometimes" references can prevent this and sometimes not. Just not good enough, while syntactically making it harder to tell that references are being used in function calls and such.
- cousin_it 10y agoReferences are crazy. There's no simple guideline to use them safely. Check this out: const string& s = max<string>("a", "b"); cout << s; Standard types and functions, const references, no warnings, everything by the book. But it crashes. There's a similar can of worms related to slicing (treating Derived* as Base* is silently unsafe because arrays and copy constructors exist). C++ is scary.
- qcoh 10y agoWorks for me in both C++03 and C++14? https://ideone.com/KhfGaN https://ideone.com/KhfGaN
- cousin_it 10y agoYour code is not the same as my code and you're probably new to C++. I suggest trying two changes: 1) Replace max("a", "b") with max("b", "a"). Run it, look closely at the result, and be enlightened. 2) Replace max("a", "b") with max<string>("a", "b"). Run it, look at the result, and be enlightened in a different way. As a small hint, your code invokes unspecified behavior, while my code invokes undefined behavior. If this exercise doesn't put the fear of C++ into you, then I don't know what will :-)
- qcoh 10y agoSorry, can you clarify? Adding the template argument causes a crash on idone, but I have no idea why that happens. Moreover, the output on my computer is always b\n. EDIT: Not sure if this is it, but after some digging I've found [0] on lifetimes of temporaries. As for the lacking template argument, presumably const char* is inferred and a temporary string is constructed in main? [0] http://stackoverflow.com/a/2784304 http://stackoverflow.com/a/2784304
- dragontamer 10y ago> Arguably the std::move stuff and the semantics around it have added uncomfortable complexity to the language Compared to using std::auto? Nah man. std::move is far easier to understand than trying to shove both concepts down operator=. std::unique is simply easier to use than std::auto by any real measurement.
- cmrdporcupine 10y agoMy perhaps naive uneducated opinion: they should have added syntactic sugar for it instead of making it look like a function. A new operator perhaps.
- dragontamer 10y ago> A new operator perhaps. That'd invalidate a lot of programs. Just like how the new _ operator in Java is killing a lot of code right now. Every symbol that can be used... has been used. "Move" is the meaning of the operator in any case. Its no less readable than say... Python's __lshift__ or __add__.
- sqeaky 10y agoWhat if a hypothetical new operator used only characters already not allowed in identifiers and composed of characters not allowed in contiguous operator sequences. Consider: :=, <-=, <|, $,
- cbsmith 10y agoAFAIK, _ isn't an operator in Java (http://docs.oracle.com/javase/specs/jls/se8/html/jls-3.html#jls-3.12 http://docs.oracle.com/javase/specs/jls/se8/html/jls-3.html#...). The language does allow _ to be used as a separator in numeric literals. Is there a new operator in some upcoming version of the language specification?
- yrro 10y ago_ is a keyword in Java 9.
- unscaled 10y agoThe 'original sin' of C++ is not having both references and pointers, but trying to remain compatible with C. That means that Stroustrup couldn't have ditched pointers, but if he wanted to implement operator overloading for value types with same efficiency as C he needed some way to support doing that without using pointers directly: http://www.stroustrup.com/bs_faq2.html#pointers-and-references http://www.stroustrup.com/bs_faq2.html#pointers-and-referenc... http://stackoverflow.com/questions/8007832/could-operator-overloading-have-worked-without-references http://stackoverflow.com/questions/8007832/could-operator-ov... A lot of the nastiness of C++ comes from trying to maintain C compatibility: Special behavior for POD, most vexing parse, .h/.cc file split, everything preprocessor and many other misfeatures. Of course, there's also the flip side of it: if C++ wasn't a near-superset of C, it would have never become so popular.
- cmrdporcupine 10y agoYeah I agree that C compatibility is the fail. But it's also the only reason C++ is around today. There were plenty of other compiled systems languages with OO or module systems floated in the 80s, including the quite capable Modula-2/3s, various Pascal dialects, etc. Of all of them only C++ (and Objective-C because iOS) has any relevance today. And likely because there was a kind of path to trendy-OO for existing C programmers and shops.
- cbsmith 10y ago> Arguably the std::move stuff and the semantics around it have added uncomfortable complexity to the language, even if it might have been necessary. I think the main pain with move semantics was that it had to be bolted on to the language later. Consider that factor, they did an amazing job with it.
- Koshkin 10y agoNo, the original sin was C's and it was the use of a separate "arrow" operator for accessing fields of a struct through a pointer. Other languages (C#, Java) successfully avoided that. C++ references allow templated code to be agnostic of whether an object is given by value or by reference, because the syntax is now more uniform.
- huhtenberg 10y agoI started with C++ around '92, but you are right, C++ was already complex back then, but it was still manageable, you could, sort of, hold its larger picture entirely in your head and still had some elegance to it.
- banachtarski 10y agoAs a dissenting opinion from someone who has not quite 20 years of experience with C/C++ but 14, modern C++ is what brought me back to the language. The idioms allow me to use the language more functionally. More importantly, the STL is actually usable now and you can just code using it and std::algorithm without thinking of the nuts and bolts of move assignment and stuff at all. In the case where you need performance, you have the paradigm at your disposal. Also, you have a ref counted pointer thread safe pointer! A means of transferring ownership so heap allocated objects can also follow the same RAII pattern! I have a much easier time holding the entire picture together but others are correct in saying you have to throw away old conceptions and ideas of the language. I personally have thrown away the way I think about C entirely (at least when I code in modern C++).
- kazinator 10y agoC++ still cannot compete with proper high level languages in this area. C++ is precisely useful because it's a better C: it has a C-like subset, and works directly with C data structures and function call interfaces. Plus it has features to do that kind of programming in a safer, more managed way. I would absolutely not use C++ for anything that isn't in the area of "this should be ideally written in C, but that would bring in undue difficulties, risks and time". The more C++ moves away from its focus of being a language for writing low-levelish systems middleware, the more it loses its relevance. > Also, you have a ref counted pointer thread safe pointer! Had those in 1997 using draft ISO C++, thanks.
- MichaelMoser123 10y agojust be careful with those ref counted pointers: shared_ptr/weak_ptr introduce a lot of atomic variable access. if you use them from a single thread then that's pure overhead.
- deong 10y agoThe thing is that something like computing primes with template instantiations was always a little weird and came with problems around sensible build processes. The rules were...well...complicated, but not that complicated. You had ADL rules that were weird, but basically, you could view a lot of the template hackery as a weird thing that you could understand without too much trouble, but probably didn't need to. That's much less true with most of the C++-11/14/17 features. C++ has always been complex, but I defy anyone to read "Effective Modern C++" from cover to cover without clutching their pearls in horror. Don't get me wrong, I actually like C++ in a weird way. There's nothing else out there that goes quite so far in putting you the programmer in charge while still attempting to let you work at a slightly higher level. But the price in cognitive load for this flexibility is just stupidly high.
- cbsmith 10y ago> C++ has always been complex, but I defy anyone to read "Effective Modern C++" from cover to cover without clutching their pearls in horror. Actually, Effective Modern C++ was pretty awesome. A good guide through all the complexity.
- deong 10y agoOh, the book is awesome. I mean the fact that such a book had to be written is a pretty rough statement about the language.
- kazinator 10y ago> I find C++ today to be far simpler, more expressive That is logically impossible, since it hasn't removed anything. The myopic mantra "just don't use what you don't like" breaks down as soon as you work in a team, where you don't dictate what other's cannot use (let alone what code they inherit).