by rubyn00bie · 5y ago · 0 comments
> There’s a pretty straightforward modern language in the middle of that, and not even a huge one.
made me literally mutter "woah... really?" I've always had the impression, and granted it is based primarily in ignorance and group think (looks around), that learning C++ is an endeavor that takes years because the language is so complex. And so, I would love any resources that make it more tractable... or does it really boil down to don't use "obscure" features you don't need?
Then read some code and write some code. Don’t worry about efficiency. You can read the “core guidelines” as well which are quite useful, but for a complete beginner many of them will be more than you need.
Like Lisp, C++ is a multi paradigm language: you can write procedural, o-o, and other styles of code. It has a pretty powerful type system.
Don’t worry about old, c-inspired c++. If you know C, as a learner consider c++’s relationship with C to be no different from Java’s or even Python’s relationship with C: some common syntax and conventions. Of course C++ is indeed more tightly coupled with C, but if you think of it that way you will make your life significantly harder, especially when starting out.
I find c++ pretty convenient for rapidly prototyping something that runs reasonably quickly. Then if I care I have enormous power for optimizing...but it’s often unnecessary.
C++ Core Guidelines: https://isocpp.github.io/CppCoreGuidelines/CppCoreGuidelines
GSL (Core Guidelines Support Library): https://github.com/Microsoft/GSL
With real code (with a build system, various includes, and likely your employer's proprietary code you aren't allowed to upload to some web site) it's usually far simpler to simply use -S or --save-temps. Sometimes you can make a toy case or take a sample from your employer's code base and paste it into godbolt.
It's very rare for most users to want to see the assembly code.
Maybe, but if I'm hiring a C++ programmer I'd expect them to want to do this occasionally.
Pick an interesting project and continue in that vein. The biggest difficulty for me was understanding the memory model and all the different types of value, references and so on, but #C++ was again a brilliant resource for smart pointers to good articles on the difficult subjects.
Professional C++ programmers might have other opinions on how best to learn.
When people judge C++ for its complexity - there's a reason for that. C++ is both a syntax and a program. The program is the compiler, and the syntax is the part of the compiler that parses your text file to produce machine code.
What you want to do to learn programming, is first understand why do you want to learn programming and what kind of thing you want to create. Everything you need to know to not write obscure programs in C++ is to learn C. C can provide with every basic concept you need in order to be able to program anything that you want. Maybe, if you invest enough time and dedication, you'll realize that all these fancy features that C++ implements are unnecessary.
Learn operations on strings, dynamic memory management with boundary checking, control flows, simple and abstract data types, etc. But, most importantly, you should have a goal - an idea of what you want to accomplish with that knowledge, otherwise learning the syntax of the language and its "cool new features" will leave you thinking "welp, what do I do now".
This is very bad advice. C lacks fundamental features like unwind-protect (called RAII in c++ circles) and any metasyntactic constructs at all, all of which (among other things) help you avoid boilerplate (a prime source of bugs).
I write C code, and there is a place for it. I write a lot of Lisp code too, as there is a place for that. But nonsense like the quoted comment is like saying you only need to speak English, or the only vehicle you need is an automobile.
Culturally, we're at the place where most of people prefer higher-level things that are "easier". The growing complexity of the C++ makes some people who prefer to work on higher-level languages anxious to consider the lower-level languages like C++.
What I was trying to convey is that C is a good start for fundamental concepts of programming and everything you need to start programming in C++ - if you want to specifically use that for your tasks. However, people are different, goals are different, we don't know what are these projects. If you're using C++ to build small programs and you don't care about efficiency - I can only shrug and say "well, you do you".
Again, I did not say "don't use any of C++ cool features". It would be even easier to grasp the complexity of C++ once you understand what's the root of the problem, why people even come up with these features, and why do you want to use them.
In your reply you just jump to conclusions, saying that "no RAII is bad" to someone who probably may never need it.
> Maybe, if you invest enough time and dedication, you'll realize that all these fancy features that C++ implements are unnecessary.
It's true, you can write anything you could write in C++ in a universal Turing machine. I don't write a lot of assembly any more and haven't written microcode in decades but sure, I could write a web server in assembly code.
unwind-protect is thankfully pretty fundamental to avoid typical resource problems; its more than syntactic sugar. When I started writing C code around 1982 it was the thing I missed the most. Counted strings not only avoid a huge number of security problems and bugs, but have been known to be significantly faster than delimited strings since around the same time.
But sure, if you don't care for them don't use C++.
Learn operations on strings, dynamic memory management with boundary checking, control flows, simple and abstract data types, etc.“
_If_ you start with C, I would skip the “operations on strings” part. C doesn’t have strings. It has a set of poorly designed functions that operate on sequences of contiguously instances of char* that satisfy the constraint that they contain exactly one char* whose value is zero: the last char in the sequence.
Nowadays, I wouldn’t start with C, though. I think it’s more distant from modern C++ than python, go, or scala (yes, you can write C++ in C style, but you shouldn’t)
>C doesn’t have strings. It has a set of poorly designed functions that operate on sequences of contiguously instances of char*
Which reads as "C doesn't have strings, it has strings". You may have confused "std::string", which is a container, with the concept of strings as "array of characters". If so, then I'm clarifying, that I've meant the latter.
If you wanted to emphasize that "char" is worse than "std::string", then I do not agree with you. The reason is that, well, as I already said - "std::string" is a container, which means using it causes unnecessary inefficiency. How do you think it was implemented? C++ was written in C, by using its functions and data types.
Your phrasing of what "char
" is reads like it is a some sort of a problem, however it's the way that things work on low level and by saying>I would skip the “operations on strings” part
you would mean that you wouldn't want the person, whom I was replying to, to know about what you've wrote about "char*".
>Nowadays, I wouldn’t start with C
We don't know who you are, and what kind of programming you do, and how much you care about quality programming. The person I was replying to seemed to be interested in lower-level programming languages, so I assumed he's a some sort of a programmer who wants to understand if it's possible to write in C++ without the "Modern" stuff (which is possible, and this is what expert programmers that write quality software do).
Idiomatic C code is an unoptimizable mess consisting of layers of `void*` indirections, violations of strict aliasing, project-specific linked list structs and poor reimplementations of other basic data structures, `strdup()` and fixed arrays everywhere... 99% of new C projects written after 2010 and outside of the field of embedded development are made by fetishists and are neither quality software engineering nor performant, but the people who still do this stuff when they're already in a .cpp file are even worse.
Sorry, that reply of mine was rude. I get where you're coming from. C is a small language that can be quickly learned by a beginner, and familiarity with much of it is still unavoidable in C++. There's nothing wrong with enjoying the C language. I must assume that you have good reasons for avoiding modern features in your particular projects. I got triggered by that statement because of some code I've been working with recently.
std::string is a particular data structure, consisting of a pointer to a heap allocation, a length value, and a capacity value; or alternately a "small string" that fits into the std::string structure itself.
There is nothing inherently inefficient about using std::string compared to implementing a similar data structure by hand; in that sense it's a "zero-cost abstraction". You can benefit from doing it yourself if you want a different data structure that provides a different feature set, such as if you don't need dynamic resizing, or if you want to refer to existing memory directly rather than giving each string its own heap allocation. But the data structure implemented by std::string is a reasonable choice for lots of scenarios. (Alternately, you might be able to just write a more efficient implementation of std::string than the implementation in whatever standard library you're using, but most standard libraries are reasonably well optimized.)
> How do you think it was implemented? C++ was written in C, by using its functions and data types.
All C++ standard library implementations, and most C++ compilers, are written in C++. The standard library does of course use primitive types such as pointers, which are shared with C, but that's not the same thing as being written in C. That said, most C++ standard library implementations do use some C standard library functions, though they don't have to. In particular, std::string implementations are likely to use memcpy/memmove to copy strings, and malloc for the heap allocations.
But they don't use the 'str' family of C standard library functions, the functions one would typically learn about when learning how to use strings in C. They can't, since those functions don't handle interior nul bytes, which std::string is required to support. Those functions are also unnecessarily inefficient when you know the string length, as std::string does.
> write in C++ without the "Modern" stuff (which is possible, and this is what expert programmers that write quality software do).
Expert C++ programmers know which parts of "modern" C++ have zero cost and can be used freely, which parts have low but acceptable cost compared to the increased memory safety and ergonomics, and which parts are best to avoid. Programmers who avoid the modern stuff entirely are mostly those who don't know C++ very well.
That said, C++ is a convoluted language that is unnecessarily difficult to become an expert in, and unnecessarily difficult (but not impossible) to write efficient code in even if you are an expert.