C++ Weekly - Ep 463 - C++26's Safer Returns

  Рет қаралды 5,738

C++ Weekly With Jason Turner

C++ Weekly With Jason Turner

Күн бұрын

Пікірлер: 46
@ywzo
@ywzo 7 сағат бұрын
Returning a reference to a temporary is not "bad practice", it's a bug.
@pdwarnes
@pdwarnes 10 сағат бұрын
I love -Werror, except in that 20 year old code base with 35,000 warnings.
@mytech6779
@mytech6779 54 минут бұрын
Yeah that's what happens after procrastinating for 20 years. An expensive piper to pay.
@TsvetanDimitrov1976
@TsvetanDimitrov1976 21 сағат бұрын
Should be an error even in c++98 imo.
@hangin10
@hangin10 19 сағат бұрын
Along with "control reaches end of non-void function"
@literallynull
@literallynull 16 сағат бұрын
I wonder how many codebases still use -std=c++98 even on modern compilers
@raymundhofmann7661
@raymundhofmann7661 13 сағат бұрын
Does anyone have a reason why it still is a warning, along with the "not all paths return a value" in non void functions generating invalid code? If one wants to produce such a construct, explicitly enable these in the command line, do it in assembly or use compiler specific attributes or something like that. Warnings about relevant UB that unexpectedly optimizes stuff away might also be appreciated.
@oracleoftroy
@oracleoftroy 14 сағат бұрын
I feel like Werror is in conflict with having a pedantically high set of warnings enabled. I'd rather have the strict warnings not stop me from working on code while i figure out the best way to deal with it. Take promotion and conversion warnings. If those are made herd errors, it becomes way too tempting to silence them with a cast instead of thinking through why they are showing up and what needs to be done about it. I'd rather turn off the warnings at that point as I can reenable them to see where concerning parts of my code are at any time. The moment the cast is there, it becomes way more painful to find them again. I do strive for zero warnings in my code, and I have selectively made particular warnings errors where I feel there is zero excuse for it.
@Minty_Meeo
@Minty_Meeo 17 сағат бұрын
I agree with Werror= for specific warnings that are egregious and must be addressed, but the blanket Werror for all warnings enabled is opening a project up to build errors resulting from pedantic warnings that didn't exist at the time of writing.
@szaszm_
@szaszm_ 13 сағат бұрын
This. Moving to a new major compiler version usually means I have to patch out Werror from any libs that have it, because there is a new warning, and the old code obviously didn't yet fix the warning just introduced by the new compiler. So my approach: don't make Werror the default in your CMake build, but enable it in your dev environments and CI, for your own code only.
@victotronics
@victotronics 12 сағат бұрын
That's the same point I was making in my toplevel comment. Thank you.
@organichand-pickedfree-ran1463
@organichand-pickedfree-ran1463 6 сағат бұрын
Werror is definitely annoying when you are writing new code. You can disable --Werror for development, and just use it in CI (to block merging), release builds or on demand.
@kyoai
@kyoai 19 сағат бұрын
I wonder why that hasn't been an error all along. Is there any legit reason on some niche architecture why anyone would want to return a reference to a temporary?
@Daxwax101
@Daxwax101 14 сағат бұрын
To preserve backwards compatibility to the point where it hindersnthe progress of the language... I guess?
@abt8601
@abt8601 8 сағат бұрын
I guess the reason is that it allows simple implementations without dataflow analysis capabilities to be written. Determining if a program will/might return a reference to a temporary requires such analysis.
@Mozartenhimer
@Mozartenhimer 12 сағат бұрын
I put -Werror for no return in a returning function, will do it for this too.
@sjswitzer1
@sjswitzer1 17 сағат бұрын
-Werror is a great idea in theory but when doing cross-platform development it becomes quite tedious to satisfy multiple compilers’ whims about what to issue warnings on.
@kuhluhOG
@kuhluhOG 14 сағат бұрын
And that's why you can specify specific warnings which should be treated as errors (or the other way around).
@victotronics
@victotronics 12 сағат бұрын
@ Just try to get those warnings into a cmake setup from some package you've downloaded and which should "just" install.
@teymurheydarov4873
@teymurheydarov4873 15 сағат бұрын
Will you join the upcoming event in Haggenberg, Austria this week?
@victotronics
@victotronics 2 күн бұрын
Excellent idea to have this be an error. And I still don't like "-Werror". I've seen cases where my compiler had "this is potentially confusing" warnings on stuff that was perfectly legal and well-defined. But I couldn't build that code because (I think) there is no way to tell CMake not to use "-Werror".
@manuelbergler6884
@manuelbergler6884 20 сағат бұрын
Depending how `-Werror` is added to the compiler command line in the CMake project it can be disabled. If the project uses the CMAKE_COMPILE_WARNING_AS_ERROR (or sets the COMPILE_WARNING_AS_ERROR on individual targets), then it can be temporarily disabled by using `cmake --compile-no-warning-as-error` when configuring the project. If someone hardcoded `-Werror` in a toolchain file or a `compile_options` call then you're indeed out of luck. But that is not a problem with `-Werror` per se, but rather the problem that some developers force their preferred settings onto consumers.
@DaphnePfister
@DaphnePfister 20 сағат бұрын
If its not an error, just annotate the code to ignore the warning. I use this all the time with -Wall -Werror -Wno-unknown-warning-option -Wno- #pragma gcc diagnostics push #pragma gcc diagnostics ignored "-Wsome-error" ...code the triggered the false positive... #pragma gcc diagnostics pop This has the added benefit that during code traces its both an aknowledgement that the error has been consider, and a code smell if there happens to be an observed bug that appears related to the code in case the compilers warning was actually correct. If its 3rd party code, then make sure that its set to ignore errors in 3rd party headers, eg -Wno-system-headers and make sure system headers are properly defined and included using angled brackets.
@victotronics
@victotronics 18 сағат бұрын
@@DaphnePfister That's such a headache if it's a code that I download. If a new version comes out I want my install script to work just the same. So I actually have install scripts that run an inline "sed" to patch the code before or after installation.
@victotronics
@victotronics 18 сағат бұрын
@@manuelbergler6884 Oh absolutely. I blame those developers. I'll remember that commandline option for next time. Thanks.
@toby9999
@toby9999 12 сағат бұрын
I'm going to be honest, but Im going to be very blunt... CMake is garbage.
@zamf
@zamf 4 сағат бұрын
Is it possible to enable -Werror for a specific type of warning? I think most people who are reluctant to use -Werror do so because of a few warning types that are outside of their control and would ruin the whole compilation if -Werror was enabled. In other words, 99% of warnings can be treated as errors and fixed but because of 1% of warnings that are known not be real bugs but for some reason cannot be fixed all the warnings are ignored. If there is a way to enable Werror for the 99% of the warning types and let the other 1% still be warnings this would help weed out the unnecessary warnings. Of course, 99% and 1% are arbitrary numbers that I chose. Maybe in real life it's more like 90/10 or 80/20 or something like that.
@Polynuttery
@Polynuttery 3 сағат бұрын
After a year of Rust, I hope I never have to use C or C++ ever again.
@mytech6779
@mytech6779 43 минут бұрын
So why are you still here? Don't let the door hit ya'.
@KX36
@KX36 17 сағат бұрын
My only problem with Werror is it slows me down too much when I'm trying to quickly get the skeleton of my idea onto the screen. I would definitely use it for production code.
@__Brandon__
@__Brandon__ 13 сағат бұрын
Don't you just start writing the correct thing the first time after a while. Sorta like any other kind of compilation error
@01rnr01
@01rnr01 20 сағат бұрын
notice how this formatting style hides the fact it’s a reference being returned ;) (i.e. compare with “const int& get…” )
@oracleoftroy
@oracleoftroy 15 сағат бұрын
You mean, compared to: auto get() -> const int & :)
@01rnr01
@01rnr01 7 сағат бұрын
@ or the one we actually see on the screen xD
@Krawacik3d
@Krawacik3d 19 сағат бұрын
Looking at this from a different angle, most large-scale project will never switch to modern C++ versions, because fixing all of those warnings will be considered too expensive/unnecessary.
@carlosgalvez7464
@carlosgalvez7464 19 сағат бұрын
This is a step in the right direction, but still not enough: you can still disable the error via -Wno-. Which people who don't use -Werror will do. Compiler developers get a huge pushback on any type of hard error that cannot be disabled due to backwards compatibility.
@Minty_Meeo
@Minty_Meeo 17 сағат бұрын
But how else will I impement my random number generator function that uses garbage on the stack for entropy? /s
@carlosgalvez7464
@carlosgalvez7464 17 сағат бұрын
@@Minty_Meeo Typically this is done with -f flags, like -fwrapv. Having this as an error that behaves like a warning is confusing :/
@toby9999
@toby9999 12 сағат бұрын
Do we really need that much hand holding? The whole "make it safe" fanaticism is becoming tiresome.
@Minty_Meeo
@Minty_Meeo 10 сағат бұрын
@ What is wrong with something you should absolutely never under any circumstances do being turned into an error?
@toby9999
@toby9999 12 сағат бұрын
Why would anyone return a reference to a temporary anyway? It makes no sense.
@Omnifarious0
@Omnifarious0 20 сағат бұрын
I wonder about returning references to local variables. That, I think, is just as bad.
7 Outside The Box Puzzles
12:16
MindYourDecisions
Рет қаралды 282 М.
C++ Weekly - Ep 462 - C++23's Amazing New Range Formatters
7:23
C++ Weekly With Jason Turner
Рет қаралды 8 М.
Sigma Kid Mistake #funny #sigma
00:17
CRAZY GREAPA
Рет қаралды 30 МЛН
Beat Ronaldo, Win $1,000,000
22:45
MrBeast
Рет қаралды 158 МЛН
Chain Game Strong ⛓️
00:21
Anwar Jibawi
Рет қаралды 41 МЛН
To Brawl AND BEYOND!
00:51
Brawl Stars
Рет қаралды 17 МЛН
Inside the V3 Nazi Super Gun
19:52
Blue Paw Print
Рет қаралды 2,2 МЛН
C++ Insights - Episode 41: How type-traits work
6:44
Andreas Fertig
Рет қаралды 3,9 М.
Make Domain Rules Explicit In Any Business Application
9:23
Zoran Horvat
Рет қаралды 13 М.
How C++ took a turn for the worse
5:03
Code Persist
Рет қаралды 334 М.
C++ Weekly - Ep 460 - Why is GCC Better Than Clang?
17:31
C++ Weekly With Jason Turner
Рет қаралды 22 М.
Rust vs C++
7:18
conaticus
Рет қаралды 104 М.
2 Years of C++ Programming
8:20
Zyger
Рет қаралды 272 М.
C++ Weekly - Ep 456 - RVO + Trivial Types = Faster Code
10:33
C++ Weekly With Jason Turner
Рет қаралды 15 М.
Sigma Kid Mistake #funny #sigma
00:17
CRAZY GREAPA
Рет қаралды 30 МЛН