'BEST C++ CODE ever written' // Code Review

preview_player
Показать описание


💰 Links to stuff I use:

This video is sponsored by Brilliant.
Рекомендации по теме
Комментарии
Автор

What do you think, should we keep going? 👇

TheCherno
Автор

I think we can all agree this is some of the C++ code ever written.

not_ever
Автор

Guy knows how to bait you into a code review 😆

nexovec
Автор

Everyday I write the best code I've ever written. Everyday I also think yesterday's code is the worst I've ever written

chudchadanstud
Автор

26:00 There is actually a really OP keyword for this called 'consteval' that enforces a function call to be comptime and prevents runtime execution.

cubemaster
Автор

For me a 'using namespace' in a header file is absolutly forbidden.

MapleLeavesInAutumn
Автор

"and used modern C++ features" looks like perfection is out of the window

TeslaPixel
Автор

16:30 I took an audio programming class in college. I used msvc as my compiler (c++20), the professor used a compiler on Linux (c++11). I did not specify std:: before using size_t or for some other common integer types such as uint32_t Everything compiled fine on my machine but not the professors. The only issue in my project was that. I ended up being docked 10 points on the first assignment. I would say it's good practice to access the data types this way, as clearly outlined by the standard, because youll never know when you'll get burned.

Edit to clarify: ANY warning or error in any of the projects (equivalent of /W4 on any compiler) resulted in a 10 point hit to the project grade, no matter how small. The class rules were just strict

JasonGrace
Автор

It's so good it's classified as C+++

tedchirvasiu
Автор

11:28
If I remember Sebastian Lague's videos correctly, this is actually common practice for chess engines. I think it allows for some neat tricks when calculating moves, but I'm not entirely sure anymore. But I'd highly recommend his videos on that

DeGandalf
Автор

the std:: qualifier on sized arithmetic types will become more prevalent as people start using "import std" since the unqualified names are only brought by importing std.compat.

isodoubIet
Автор

"modern C++ features" and "updated the C code to use object-oriented programming" aren't exactly a telltale sign of perfect code. Not that they're bad, but you don't need them to write good C++. All you need to write good C++ is "solve the problem you have given the constraints of the platform" as Mike Acton would put it.

I agree that moving as much of the program as possible onto the stack is definitely a solid move.

imphonic
Автор

Pragmas are never added to the standard, pragma is by definition something OVER the standard, some compiler specific extension, even if it becomes a quasi standard.
But sure, they COULD add some preprocessor directive to replicate the functionality. I just think they focus on making modules work, which makes header guards and pragma once unnecessary in the vast majority of cases.
20:40 I would definitely use some type that actually stores the chars locally, like a char[2] or an array<char, 2>. If this table is referenced a lot then I don't want to rely on the compiler to place the string literals close to each other, I want a guarantee that these 64*2 bytes are right next to each other and always hot in the cache. Also sparing a lot of indirection.

Definitely continue, I wanna learn from this master of C++.

ciCCapROSTi
Автор

constexpr = this value is known at compile time
const = this value doesnt change, but its computed at runtime
the compiler will probably be able to optimize it anyway but constexpr is what i use in such situations

Timm
Автор

15:40 Using namespace is a big no-no in header files, except for very specific things if inline namespaces and namespace aliases don’t do the job or you can’t use them. Example:
namespace my_namespace { using namespace other_namespace; …other sutff… }

In his case, he doesn’t even need the "sv" literals, his array is constexpr initialized, so there’s nothing to gain there even hypothetically. The string_view objects are created at compile-time and they’re the same no matter if he uses sv or not.

Bolpat
Автор

This is the best comment i've ever written

ssgroast
Автор

the const strings: they are not modifiable.
They are declared "const" and trying to modify them will (should) give you a compiler-error. Further the compiler can and depending on the situation will make a string point to readonly-memory without copying anything.

ABaumstumpf
Автор

16:00 What do I think? I think you should put the constants in a namespace inside the header file, and put the "using namespace" inside that.

That gives you the brevity that you want, and the "using" doesn't leak.

DeGuerre
Автор

Regarding the character lookup table, in C99, an array initializer can use designators to put values at specified indices. AFAIK, it was never brought into the C++ standard but constexpr gives more flexibility when initializing static data anyways.

AlexandreJasmin
Автор

Regarding the stringview array: It might be easier to make the array a single long string and index by i/2. This removes the layer of indirection imposed by stringviews/const char* and keeps the data near thereby keeping the memory 'hot' allowing better caching

commanderguyonyt