@@EasyElectronics2412 Well, no, they can be any expression that yields a value of the correct type (int, in this case) so can come from anywhere: other non-const ints, function, macro, constant, or otherwise.
Deleting/destructing an ostream has sideeffects, namely flushing the rest inside the buffer. If you forget to delete/close the stream, the file contents might not be completely written to the file.
A suggestion for a future video perhaps: C++ intro for C programmers (stuff that can be done better or differently in C++ and that C programmers would find helpful)
@@JacobSorber That can be surprisingly hard, because C++ is not a simple extension as it might seem. It requirec somplete paradigm shift and different thinking. There's a really good chamce a C programmer will write a terrible C++ code that still may compile.
I'm only now realizing I'm a year late on this one. But there's a great intro video from Bisqwit called "C++ for C programmers". It is mostly a summary, and no further videos in the series have been made yet, unfortunately. He pretty much explains what we'd now call "C with C++ features". I def. recommend
I use the ternary operator to safe wrap non safe functions, ie: return str == NULL ? 0 : strlen(str); it’s a good fit for wrapping functions that can be a termination point
Thia channel is a RUclips treasure. I must have shared it with dozens with my friends! Thank you prof for the effort and generosity. I am getting a co-op next term and will definitely join your pateron 😁.
one of the ways I explain it as a true/false return. it will return first part if true, second part if false. I usually show people this (in php) $strSuffix = ($countPeople == 1) ? "Person" : "People"
explainning C code - use ternary operator for max(). explainning c++ code - ostream, refernces, pointers, memory leaks. although you probably could use the occasion to introduce sprintf() and the rest of the family.
I'd personally not use a ternary expression to assign the local variable, but rather a function that contains the code (in either form). The point being that the name of the function should be enough (if chosen well) to understand what the code does. That's how I make code more readable, even though it requires to jump to the definition to said new function (virtually free with the right IDE)
4:26, sorry but that's still fine in a ternary, just needs formatting, I tend to put large expressions on multiple lines with the brackets on separate lines also, even tabbing the contents forward to make clear it's part of the expression, although I would not normally stick a loop in there, rather I didn't even realise could put loops in expressions, how would the compiler know what value to give it in that situation?
I wasn't able to find your video on memory leaks and why they aren't always important. Could you possibly share a link to it either in the video description or here in the comments?
Also. If using ternary operators with printf or other formatting funcs there is no need for the result variable: printf("max of %d and %d is %d. ", num1, num2, num1 > num2 ? num1 : num2);
Very true, though the more we try to do on one line...well things get harder to visually parse. This one is pretty straightforward, but there's a risk of readabaility issues.
An assembly output comparison of the two programs would be interesting to discuss. Is using the ternary operator more efficient than an if-else statement. If the answer is obvious, sorry for wasting your time... Thanks
Just quickly ran gcc -S on both versions of the C program and the compiler output on my machine was the same-verbatim. Is that to be expected? Is it a similar situation to comparing and infinite 'for' loop and a while loop?
Hi Tolis! It is interesting to look at. I didn't both because of time and because it's really going to be compiler (and compiler-option) dependent. Maybe something to look at in a future video. Thanks!
@@JacobSorber I heard somewhere that the ternary operator can mess with RVO and can sometimes confuse the compiler. That might be something to consider when dealing with objects that aren't trivially copyable...
Regarding the memory leak. GCC has builtin address sanatizer and leak detector so freeing the memory the to be preferred because it will complain about it and always having a certain number of "stuff we do not deallocate" is inconvenient.
hay jacob can you talk about data serialization/encapsulation when sending data over socket. (i want to know how can we send whole struct over socket).
And another great thing about ternary is that it can be used to initialize const variables:
int const themax = (num1 > num2) ? num1 : num2;
Num1 and num2 must be macros or constants
@@EasyElectronics2412 Well, no, they can be any expression that yields a value of the correct type (int, in this case) so can come from anywhere: other non-const ints, function, macro, constant, or otherwise.
@@EasyElectronics2412 making it const just means you cant change it not that it has to be a compile time value
@@homelikebrick42 true i got that😅
Deleting/destructing an ostream has sideeffects, namely flushing the rest inside the buffer. If you forget to delete/close the stream, the file contents might not be completely written to the file.
Ah, good point. I was just thinking about memory clean up. Thanks.
A suggestion for a future video perhaps: C++ intro for C programmers (stuff that can be done better or differently in C++ and that C programmers would find helpful)
Thanks for the idea. I'll see what I can do.
@@JacobSorber That can be surprisingly hard, because C++ is not a simple extension as it might seem. It requirec somplete paradigm shift and different thinking. There's a really good chamce a C programmer will write a terrible C++ code that still may compile.
I'm only now realizing I'm a year late on this one. But there's a great intro video from Bisqwit called "C++ for C programmers".
It is mostly a summary, and no further videos in the series have been made yet, unfortunately. He pretty much explains what we'd now call "C with C++ features". I def. recommend
I use the ternary operator to safe wrap non safe functions, ie: return str == NULL ? 0 : strlen(str); it’s a good fit for wrapping functions that can be a termination point
Thia channel is a RUclips treasure. I must have shared it with dozens with my friends! Thank you prof for the effort and generosity.
I am getting a co-op next term and will definitely join your pateron 😁.
You're welcome. I'm glad I could help.
I think I first learned about the ternary operator alongside trigraphs in a presentation on fun tricks to confuse your TAs.
one of the ways I explain it as a true/false return. it will return first part if true, second part if false. I usually show people this (in php)
$strSuffix = ($countPeople == 1) ? "Person" : "People"
YES, more pointer and reference coverage please.
thank you for the tip with the ternary operator used to switch between cout and ofstream based on argc. that's a really neat trick.
Cool, ternary is like the "IF" in excel... C++ looks like a nightmare... :) Thanks A.,
explainning C code - use ternary operator for max().
explainning c++ code - ostream, refernces, pointers, memory leaks.
although you probably could use the occasion to introduce sprintf() and the rest of the family.
I'd personally not use a ternary expression to assign the local variable, but rather a function that contains the code (in either form).
The point being that the name of the function should be enough (if chosen well) to understand what the code does.
That's how I make code more readable, even though it requires to jump to the definition to said new function (virtually free with the right IDE)
4:26, sorry but that's still fine in a ternary, just needs formatting, I tend to put large expressions on multiple lines with the brackets on separate lines also, even tabbing the contents forward to make clear it's part of the expression, although I would not normally stick a loop in there, rather I didn't even realise could put loops in expressions, how would the compiler know what value to give it in that situation?
I wasn't able to find your video on memory leaks and why they aren't always important. Could you possibly share a link to it either in the video description or here in the comments?
Thank you for that. Can you me out?
I need to gain some understanding of the C lang NeuraLink like devices.
Also. If using ternary operators with printf or other formatting funcs there is no need for the result variable:
printf("max of %d and %d is %d.
", num1, num2, num1 > num2 ? num1 : num2);
Very true, though the more we try to do on one line...well things get harder to visually parse. This one is pretty straightforward, but there's a risk of readabaility issues.
An assembly output comparison of the two programs would be interesting to discuss. Is using the ternary operator more efficient than an if-else statement. If the answer is obvious, sorry for wasting your time...
Thanks
Just quickly ran gcc -S on both versions of the C program and the compiler output on my machine was the same-verbatim. Is that to be expected? Is it a similar situation to comparing and infinite 'for' loop and a while loop?
Hi Tolis! It is interesting to look at. I didn't both because of time and because it's really going to be compiler (and compiler-option) dependent. Maybe something to look at in a future video. Thanks!
@@JacobSorber I heard somewhere that the ternary operator can mess with RVO and can sometimes confuse the compiler. That might be something to consider when dealing with objects that aren't trivially copyable...
@@JacobSorber Absolute Legend!
Regarding the memory leak. GCC has builtin address sanatizer and leak detector so freeing the memory the to be preferred because it will complain about it and always having a certain number of "stuff we do not deallocate" is inconvenient.
True. Good point.
My programming teacher at school referred to usage of the ternary operator a a SAOL
S-mart
A-ss
O-ne
L-iner
@10:00 @line 18th
The assign upstream &myout = *pmyout
Is not really clear to me. Why using the * and why not using the & since I just declared myout
not equal (!=) how could you write it like that (≠)in videos?
@Alfie Aha sorry I'm a notepad++ user 😀
ruclips.net/video/5td9upDbkaU/видео.html
He is using a font with ligatures
Thank you all guys for the help 💖
He talked about it in this video:
ruclips.net/video/5td9upDbkaU/видео.html
tentary is the holy operator
God is one and has no partners.
when will you talk about rust? It seems like you considered it since a few years ago but what's the status now?
hay jacob can you talk about data serialization/encapsulation when sending data over socket. (i want to know how can we send whole struct over socket).
Probably. I'll add it to the list, and see what I can do.
@@JacobSorber thank you jacob
What VSC pluggin displays "==" as crossing "="?
It's just a font ligature. If you turn ligatures on, and use a font with a ligature for "==", then magic will happen.
@@JacobSorber thanks!
Wait how do you have it auto highlighting like that?
nested ternaries ftw
the forbidden operator!
goto ternary;
😂
@@JacobSorber i sent you a mail to your edu adress. It could be interesting.
What’s with the funky != operator? I’ve been seeing that in some videos on here and other channels lately … whatever that is it should be dropped.
It's a font ligature. See my font video for more clarification.
Showing the ternary operator to the gullible can be aukward.