How to Deal with Multiple Return Values in C++

preview_player
Показать описание


Thank you to the following Patreon supporters:
- Samuel Egger
- Dominic Pace
- Kevin Gregory Agwaze
- Sébastien Bervoets
- Tobias Humig
- Peter Siegmund

Gear I use:
-----------------

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

Hope you guys enjoyed the video! Just some quick notes: the code I wrote at around 10:30 needs some changes to actually work, since we've changed the type to a std::vector. Specifically, you could write either

std::vector<std::string> results(2);
results[0] = vs;
results[1] = fs;

or just

std::vector<std::string> results;
results.push_back(vs);
results.push_back(fs);

but leaving it as I left it wouldn't work, since we'd be writing to an index outside the array. Obviously in the video I'm just trying to get the point across about using an std::vector and not really trying to write complete working code, but just in case I thought I'd leave this comment.

TheCherno
Автор

For anyone reading recently, there's a better way with structured binding since c++17:

Return a tuple of your choice and return it as per the video:
std::tuple<std::string, std::string, int> getData() {
return { "One", "two", 3 };
}
Retrieve values using structured binding:
auto [str1, str2, number] = getData();

alguienmasraro
Автор

I think it will useful to keep these "optimization" parts in your video in the context you explain as a bonus for those (like you) who love it and try to create good optimized code. May be put them at the end of the video or display a little "sign" on the screen whe you speak about optimization if someone's complaining he's lost.

nighma
Автор

at 13:20

It's worth noting that there are 2 more ways to make the code look somewhat better:

In C++11 you can use something like:
std::string vs, fs;
tie(vs, fs) = ParseShader("directory...") // tie() is in the <tuple> header

And in C++17 you can even do:
auto [vs, fs] = ParseShader("directory...")

also instead of creating 2 strings at the end you can just return make_pair(vs, fs);
or in C++17 return {vs, fs};

panagiotispetridis
Автор

i think structs and pass by reference methods are the best

Djzaamir
Автор

I personally quite like <tuple>'s std::tie function, not sure what that is like for performance but is a good way to have 2 named return types (of any type) without having go and write a struct for every function that requires multiple return types.

JackPunter
Автор

*My takeaways:*
1. Tuples 10:49
2. Pair 14:00
3. Struct 14:30

leixun
Автор

for tuple you can easily extract value like auto [vertexSource, fragmentSource]=func() in c++ 17

rv
Автор

I'm really glad you use the struct way. I'm a newcomer to c++, i used to program in Java and I solved this problem by making classes with just what variables i needed to return. I'm happy now

RespecWamen
Автор

I return std::optional with a struct. Typically you need to be able to return a valid structure or "nothing" and std::optional facilitates that.

homomorphic
Автор

I just want to say : C++17 Structured Bindings \o/

DrAuraHxC
Автор

I guess structured bindings take away most of the readability-concerns at least on the caller-side.
Returning something like a pointer to an array seems very odd to me and creates very bad habbits. That kind of usage can introduce very serious bugs, especially with inexperienced people. That's a thing I'd never recommend doing.

tehLyrex
Автор

Thank you so much for your indepth coverage of coding styles. As someone new to code you are making understanding c++ a very enjoyable experience.

Great Job Cherno!

simond
Автор

Now would be a really good time to make a video on templates, because people who know c++ templates are gonna get this, but otherwise Its must be like what's goin on man!!!!

miteshsharma
Автор

A very nice video, explanation. I would like to mention that if you are precise, the function that takes non const reference as argument and return nothing is the most performance friendly.

aubertducharmont
Автор

You can enumerate the tuple position value to make it somewhat readable - std::get<person(age)>(John) - John is the tuple and "enum person(age = 0, ...)" for the enumerator

derivepi
Автор

Interesting... I came to the same conclusions as you a few years back when checking the c++11 specifications. Thanks for informing us about your views.

sander_bouwhuis
Автор

the struct solution is definitely the best way to solve this for huge amount of variables and calling by references is the best for variabel num up to 3

manuelkoch
Автор

THE LAST OPEN-GL AND THIS EPISODE WAS VERY DIFFICULT FOR ME . JUST TO SAY THAT I FOLLOWED YOU FROM SCRATCH BUT THIS ONE WAS A LITTLE MIXED STUFF . I DIDN'T UNDERSTAND THE SYNTAX <GET> MAYBE ITS TEMPLATES...

edenpanigel
Автор

I agree with you, returning struct seems like the best way. I also like 1st two methods, passing references/pointers as inputs and making updates to them in function.
Other stuff you showed gets confusing quickly, mostly because I'm not that familiar with c++ library features. They keep adding new functionality I never seen before.

renade