std::optional In C++17

  Рет қаралды 2,566

CppNuts

CppNuts

Күн бұрын

Пікірлер: 8
@singularity6761
@singularity6761 9 ай бұрын
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👍🏻
@segfault4568
@segfault4568 9 ай бұрын
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
@SteamDeckLab Күн бұрын
Excellent explanation, thank you! What you could mention as well is the usage of std::nullopt as a best practice...
@az6876
@az6876 10 ай бұрын
Now I hope you will talk about c++23's std::expected (if you haven't already). Very useful.
@thenightwolf224
@thenightwolf224 10 ай бұрын
would you make playlist To Keep Up With You
@CppNuts
@CppNuts 10 ай бұрын
C++17 Features kzbin.info/aero/PLk6CEY9XxSIAEzHd_hTO-PRD6L8XfIUzu If this is what you are asking..
@sadudinesh649
@sadudinesh649 10 ай бұрын
can you explain std::function
std::any In C++17
6:08
CppNuts
Рет қаралды 2,6 М.
Arenas, strings and Scuffed Templates in C
12:28
VoxelRifts
Рет қаралды 99 М.
Sigma Kid Mistake #funny #sigma
00:17
CRAZY GREAPA
Рет қаралды 30 МЛН
Producer And Consumer Problem
15:42
CppNuts
Рет қаралды 38 М.
Zig For the Uninitiated: Zig Interfaces
11:56
Tyler Calder
Рет қаралды 2,3 М.
Premature Optimization
12:39
CodeAesthetic
Рет қаралды 847 М.
C++ Weekly - Ep 404 - How (and Why) To Write Code That Avoids std::move
8:50
C++ Weekly With Jason Turner
Рет қаралды 34 М.
C++ Weekly - Ep 421 - You're Using optional, variant, pair, tuple, any, and expected Wrong!
10:34
Master Multithreading : Thread Pool In C++
20:19
CppNuts
Рет қаралды 14 М.
How to Deal with OPTIONAL Data in C++
10:06
The Cherno
Рет қаралды 70 М.
Dependency Injection, The Best Pattern
13:16
CodeAesthetic
Рет қаралды 908 М.
7 Outside The Box Puzzles
12:16
MindYourDecisions
Рет қаралды 190 М.