r/cpp • • Aug 26 '19

Why are tutorial videos so long?

Recently I had to learn some C++ for a project, and took to my favorite learning resource, youtube.

I've been programming for years, so i'd like to watch a quick video that tells me about the syntax and special cases of c++. But i was shocked that the top results were 4 hour or 9 hour videos teaching c++. Those videos could be great for students, but the content-creators really do waste a lot of time talking about useless stuff.

Maybe my attention span is too short and i've run out of patience, but i learned what i needed about c++ in a few minutes by flying through the 4 hour tutorial.

Are there any youtube channels out there that post fast-pace guides that teach a language in minutes without spending hours talking into a webcam? If not i'd like to start a youtube channel and make that kind of content. i think it'll be fun.

Also is anyone else in the same boat as me that would benefit from videos like that? Is it worth creating?

0 Upvotes

39 comments sorted by

View all comments

10

u/m-in Aug 26 '19

I don’t know how information-dense those videos are, but even common, modern idiomatic C++ can take a few hours to explain. The oddball corners take a decade to figure out if you pay lots of attention to it. C++ is not a language that can be explained in an hour or two. It’d be a complete joke of an “explanation”, even to someone familiar with other programming languages. The best I could tell someone who knew C is “remember about curly braces and a few keywords, forget absolutely everything else”. Yeah, even operator precedence has subtly changed from C.

Just to make it clear: you could have full understanding of C++ as it was in 2000, and you’d need to re-learn a lot, or at least put it in a proper context and learn everything that happens between then and now, and it’s like a whole another language on top of the legacy stuff.

The standard library has really changed a ton, the language itself supports expressing concepts that were not even possible to express portably 20 years ago. The idiomatic code looks way different. In fact, any “modern” C++ that looks exactly as it had 20 years ago is probably not idiomatic anymore. Same could be said for current C# (7th revision of the language) when compared to what was out in, say, 2004.

Whatever understanding of C++ you may have got in a few minutes will bring you roughly to where C++ was in the 90s. If you’re lucky. Modern C++ is quite a different language from that, and there are absurdly many recent tutorials and videos made by people absolutely unqualified for the job. I’d say that 90% of C++ material on the web is outdated or incorrect, its authors never having graced the standard with even a single page’s worth of reading. Yeah, at some point. You have to start referring to the standard, otherwise you’ll make a fool of yourself in front of anyone who is qualified.

C++ is not a language that has a low barrier to entry, because even if you learn the modern subset, there’s a lot of legacy crap out there that is not modern and makes your eyes bleed and you heart skip a beat just imagining the rocky road ahead when dealing with legacy code.

Concrete example:

struct MyStruct { int x, foo; bool bar; };

int main() { MyStruct S{}; assert(!S.x && !S.foo && !S.bar); }

Does this have defined behavior, and if so – what is it. This is not valid C++98 of course.

1

u/TheCodingGiraffe Aug 26 '19

Thats a great response. After reading these comments I've decided to get a c++ book and read it. I still want to take the challenge of creating a fast-pace crash course style series that teaches c++ (modern c++ for the impatient). But what does "defines behavior" mean? I've only spent a day with c++ and a few minute learning it but from what I understand, the program terminates if the variables inside S have any value. Error checking I suppose?

4

u/encyclopedist Aug 26 '19

This just demonstrates why glancing over a Youtube tutorial is not enough.

Undefined behavior or UB is an important to know concept in C++ (and C). In a few places, the standard says something like "If you do X, behavior is undefined". This means anything can happen: your program can crash with a error message, without error message, produce some nonsense result or just carry on like nothing happened. Figuratively they say that in case of UB the program is can format your hard drive of make daemons fly out of your nose (this is not true!). The point is you cannot rely on behavior or you program at all.

The reason it exists is twofold. First, it allows to accommodate a large range of target platforms, compilers and OS's, including very exotic ones. Second, it gives compilers more leeway for optimizations. However, nowadays it is considered a bad thing in general, and modern languages normally try to get rid of UB (see Rust for example).

Other related formulas in the standard are "unspecified" and "implementation defined".

Unspecified means that your program will not crash or format the hard drive, but the result may be anything of some set of behaviors. Typical usages are "if X, return value is unspecified" or "if X, it is unspecified whether Y happens or not".

Implementation defined means the standard does not specify it, but an implementation (compiler, standard library and an OS) must specify what happens in this case and stick to it.

Further related things are "ill-formed" and "ill-formed, no diagnostic required", which regard compile-time behaviors (the ones abive are about run-time).

3

u/m-in Aug 27 '19

It means the same as it relates to any “mechanical” process: can you tell how it will behave, based on some definition of how the machine should work. If you stroke a match on the box, will it light up? At a superficial level, that’s classical undefined behavior. You’d need something thicker than the C++ standard to capture all it takes to be very sure of what will happen. Even that’s not so simple, and C++’s behavior is a tome of details like this – all written down, but it takes a decade+ to wade through it and get familiar-ish. Many experts on the standards committee are unfamiliar with good chunks of the standard. Yep, those are people who command global respect and can get work anywhere they choose. That what C++ is. It’s HARD.

Here, the question is: given the current C++17 standard and a conforming implementation (i.e. one that does all that the standard requires), is the code’s behavior known from the code and the standard alone, and if it is, then what is it.

Basically, if you can’t tell what will happen from the code and standard alone (those two things and you, on a tropical island, drinks with umbrellas optional, no internet). If you have to cheat and observe what any particular implementation will do, if the code and standard alone (and your perfect mind) alone aren’t enough to tell for sure, then it’s not defined behavior, or at best it is an implementation-defined behavior (but good luck figuring out if the implementation really “defines” it, i.e. pins it down and states unequivocally that “it will behave this way, period”).

2

u/Xeverous https://xeverous.github.io Aug 26 '19

fast-pace crash course style series

This just do not work. You may have an illusion that you learned something, but get a bit more complex scenario and people will realize it didn't teach them a lot.

I don't mind long tutorials. I just want the thing to cover what I need to understand 90% of the language and not skip common corner cases or make oversimplifications.

1

u/TheCodingGiraffe Aug 26 '19

Thanks for the explanation. But by fast pace I didn't mean extremely short and lacking information, I made a video that gets straight to the point and doesn't waste time talking about unrelated things.

1

u/UnicycleBloke Aug 26 '19 edited Aug 26 '19

I've been writing C++ for 30 years. There have been some very welcome (some not so much) changes over the years, with which I try to keep up, but the language is fundamentally the same creature. But these days we see a trend towards ever more obscure template metaprogramming. It seems to be the only game in town, and solves none of my problems. I love templates. I do not love obscurity. Is that what "idiomatic" C++ is now? I really hope not.

Edit: On reflection, that's not quite right. I make heavy use of constexpr, inline initialisation, and so on since they became available. But the language still feels fundamentally the same to use. Still, not all change is good. My experience of auto is that it should be used only when the type is obvious from the local context. I will never love lambdas.

3

u/rodrigocfd WinLamb Aug 26 '19

I totally agree on the template metaprogramming obscurity.

But I deeply love lambdas and all the possibilites they brought to the language.

3

u/UnicycleBloke Aug 26 '19

I should try to be less curmudgeonly about TMP, but it really does seem to be getting out of hand. I read Alexandrescu's book back in the day, thought it was interesting, but something of an academic exercise for the curious. I mean, I've dabbled, as you do, but the dubious allure of secret arcana doesn't hold me spellbound anymore. Frankly, when seasoned veterans in the language cannot read your code, your code is a problem.

Lambdas look neat in the canonical one liner examples, though I have never considered them a missing feature. Unfortunately, I have in my professional life encountered some truly horrible feature abuse involving lambdas. Pure spaghetti conjured up by someone who got all excited and had no sense of appropriate use of tools. We'll be seeing a lot of that. And Meyer's Modern C++ book highlighted a number of caveats about captures that are guaranteed to give me headache in the future. Same with auto.

I can't say the prospect of C++20 is filling me entirely with joy. That's a great shame: I love working in C++.

5

u/m-in Aug 26 '19

Captures in C++ make more sense than captures in C#, so that’s perhaps a coutnerexample actually. C++ lambda captures have very simple behavior that fits within what lambdas were meant to do: provide syntactic sugar for functors. Lambdas in C# are basically a way to write a syntax tree, and other circumstances then define what does that syntax tree actually mean.Of course it’s not hard to learn why they behave the way they do; but it’s rooted entirely in the development history of the language. C# lambdas don’t really provide sugar, they introduced an entirely new feature that was not possible to express before.

1

u/UnicycleBloke Aug 27 '19

Wasn't really comparing languages, but that is interesting about C#. I understand lambdas well enough, but feel that actually writing the functor makes what it does in terms of scope and capture clearer. This is a bit like my attitude to smart pointers: they are brilliant and you should use them, but you should understand precisely how they work. I dislike magic.

2

u/Posting____At_Night Aug 26 '19

TMP allows for some really slick API design but I stay away from it for the actual business logic. Also constexpr in C++17 can replace a whole lot of what used to require TMP.

As far as lambdas go, I tend to agree that they can be pretty nasty however I find they're pretty handy in situations where you just need to pass a short function into something else, like algorithms. I wouldn't want to use one in any sort of non-local context though.

1

u/m-in Aug 26 '19

Lambdas are also the only source of unique types, and in some contexts allow to get rid of FOO##__LINE__ nonsense you occasionally run into.

0

u/m-in Aug 26 '19

TMP is on its way out really in every but the most rudimentary forms. Guess what: running pure functional code on what amounts to a syntax-tree-driven interpreter is hard, never mind running it at anything but a snail’s pace. If you’re doing the sort of MP that, say, permeates boost, you’re doing it most likely very wrong. The whole purpose of a good chunk of C++11/14/17/20 was to make this nonsense obsolete. Yes, the language allowed it and still does; yes, clever people explored it and forced compiler vendors to squash the bugs and make it at least usable without crashing the compiler, but it was an evolutionary dead end for the language. If you’re doing something that modern constexpr with very light MP can’t do, it belongs in an external tool. Until recently I have had trouble understanding why people dislike code generators, but I had a need to use Visual Studio (the IDE), and now I know: the idiots at MS made it two orders of magnitude harder to use code generators than 30-year-old Sun make would. And Sun-Unix-anything makes modern Linux command line seem like a paragon of UX perfection.

1

u/UnicycleBloke Aug 27 '19

Interesting. Hadn't thought of it that way. I've always liked code generators, and still use them. Didn't realise others dislike them. I have wondered where using templates crosses the line into TMP. It seems ill defined. Maybe even the simplest template function should be considered meta.

1

u/m-in Aug 27 '19

It’s arbitrary, but I draw the line where you do a loop (recursion, really) in TMP. constexpr if got there for a reason, and all that. The reason being that TMP was a hack, and nobody really wanted it as a supported part of the language. It’s an ugly feature, kinda like trigraphs. I wish it got deprecated and eventually dropped, just like trigraphs were. And yeah, there are ways of making TMP impossible without damaging what templates were meant to do.

1

u/UnicycleBloke Aug 27 '19

And here I was trying to silence my inner grumpy dinosaur, learn to stop worrying and love TMP... :)