C++ Weekly - Ep 355 - 3 Steps For Safer C++

preview_player
Показать описание
☟☟ Awesome T-Shirts! Sponsors! Books! ☟☟

T-SHIRTS AVAILABLE!

WANT MORE JASON?

SUPPORT THE CHANNEL

GET INVOLVED

JASON'S BOOKS

► C++23 Best Practices

► C++ Best Practices

JASON'S PUZZLE BOOKS

► Object Lifetime Puzzlers Book 1

► Object Lifetime Puzzlers Book 2

► Object Lifetime Puzzlers Book 3

► Copy and Reference Puzzlers Book 1

► Copy and Reference Puzzlers Book 2

► Copy and Reference Puzzlers Book 3

► OpCode Puzzlers Book 1


RECOMMENDED BOOKS

AWESOME PROJECTS

O'Reilly VIDEOS

Рекомендации по теме
Комментарии
Автор

Seems I misspoke a bit in warning levels, so here's some guidelines:

* GCC/Clang/MSVC: Enable as many warnings as you can!
* Bare minimum for GCC/Clang should be -Wall -Wextra -Wconversion -Werror
* Bare minimum for MSVC: /W4 /Wx

Use /Wall on MSVC or -Weverything on Clang to discover new warnings. Enable them for short periods of time, see interesting warnings, then manually roll those into your set of warnings that you enable.

cppweekly
Автор

I'd add mandatory code review to step 0

TsvetanDimitrov
Автор

Also, don't do a debug build, but run your tests against he debug build also. Release builds generally compile out asserts, which are often used to guard against things like out-of-bounds array access (I'm thinking of Eigen in particular).

sirhenrystalwart
Автор

I ALWAYS use hand sanitizer before I write code. Better safe than sorry.

jhbonarius
Автор

Yeah, enable warning as errors and focus on fixing all the exisiting warnings in all the third party libraries you use for the rest of your life.

sheeftz
Автор

OK, but what if I don't have step 0? Where is that video?

AsmundSkjveland
Автор

I'd stick to just /W4 on MSVC. /Wall gives so many extra garbage warnings that can't be fixed. Microsofts own headers don't even compile clean on /Wall. For example, I just did it in a small project and get signed/unsigned mismatch warnings for asserts in xmemory.

mightymalakai
Автор

Fizz buzz is no replacement for fuzzing :P

samuelschwager
Автор

Just for fun I created a C++20 project in Visual Studio 2022 with this content:
#include <iostream>
int main() {std::cout << "Hello World!\n";}
I get 1 error and 112 warnings when using /Wall and /WX - so how should one deal with this? Looks like Microsoft's STL implementers didn't take your advice serious 😀

ferdistapenhorst
Автор

I tried to use Address Sanitizer in msvc. But for some reason OpenGL calls seem to piss off the Sanitizer. So I'm not sure you can use it with OpenGL.

Sebanisu
Автор

Sad that in 95% of places I have worked phase 0 was not there

marcbotnope
Автор

I still think your standard advice with warnings (even when correcting the parts you misspoke) is too lax; is there a reason to prefer -Wall -Wextra -Wconversion -pedantic -Wetc... to -Weverything -Wnoc++98-compat -Wno-etc?

danielrhouck
Автор

I think I give up on programming... I found a ctor with 37 arguments in our code base.
37 arguments is such a long list, it almost filled my screen.
And no... this is not some weird exception or special case. There are multiple devs here convinced that a high number of arguments is a sign of good code. And that long functions are good. Around 300 to 1000 lines per function. And that a parent class should know of all it's child classes...

I'm just venting. But it's hard sometimes when you have no way of changing those mindsets... Because apparently I'm the one who is wrong.

PS: The initializer list of the ctor with 37 arguments is itself 118 lines long. Because it constructs numerous complex objects.

dukex
Автор

ANOTHER episode saying “use static analysis, sanitizers, and fuzzing”?!?!

Dziaji
Автор

What if you're building in your project dependencies from source? The static analysis you're proposing might lead to a ton of errors/warnings that you have to resolve without in-depth knowledge of that dependency's codebase.

jubeidono
Автор

Those click baity titles make it hard to find content in the future. I'm all for using such titles to increase then initial views but would encourage you to rename (or append) a non click bait title a couple of weeks in

petermuller
Автор

clang-tidy is not ready for c++20 due to clang itself is not ready, it misfires when you use c++20 which is really bad

shawnshaw
Автор

Step 0

Not sure how it applies for "safe" c++.

jagansai
Автор

I am still waiting for the "replace it with rust" comment

romanstingler
Автор

I disagree with enabling warnings as errors. I'm sure you've heard all the arguments both ways, but I just think it adds no real benefit (because it's so easy to switch off when it starts to irritate you), and yet can cause major headaches when you or someone else builds your code with a different compiler version. I think it's no more effective than a 'no warnings' policy at improving code quality.

jeremystanger