Do you think it's a bad practise, to always (or let's say in 80% the cases) to use std::optional as return value? If yes why, because it's a performance overhead? Btw great channel👍🏻
@segfault45689 ай бұрын
Well personally I still prefer the old fashion way of passing in via reference for returning meaningful data and using the function's return type (bool) to check the validity/if the data being returned is meaningful or garbage. Where optional is useful is when you want to add bit of syntax sugar like a function that predicts the force required to launch a projectile to hit a certain target. (Classic Video game problem), the solution either is a float or Null if the parameters fail (Target is too far or hit scan fails or the force value returned is out of bounds). you can use optional there which would make your code very simple and concise, else you would need to check for a lot of conditions with your return value but with optional you can just return std::nullopt and forget about checking stuff outside of function scope, just use .value_or("No Result"). Think about it instead of returning -1 or some random floating value for false results you are returning Null which is a lot more easier to check and maintain. Hope it makes sense.
@SteamDeckLabКүн бұрын
Excellent explanation, thank you! What you could mention as well is the usage of std::nullopt as a best practice...
@az687610 ай бұрын
Now I hope you will talk about c++23's std::expected (if you haven't already). Very useful.
@thenightwolf22410 ай бұрын
would you make playlist To Keep Up With You
@CppNuts10 ай бұрын
C++17 Features kzbin.info/aero/PLk6CEY9XxSIAEzHd_hTO-PRD6L8XfIUzu If this is what you are asking..