How to Deal with Multiple Return Values in C++

How to Deal with Multiple Return Values in C++

The Cherno

6 лет назад

206,507 Просмотров

Ссылки и html тэги не поддерживаются


Комментарии:

@gamelovers4974
@gamelovers4974 - 15.12.2023 16:27

Totally out of my league at this time .

Ответить
@b4ttlemast0r
@b4ttlemast0r - 24.11.2023 16:02

This video seems a bit outdated even with respect to C++ 17, maybe it could be good to make an updated remake. Maybe when C++ 23 is out (idk if it has an impact on this).

Ответить
@MECHANISMUS
@MECHANISMUS - 06.07.2023 08:49

Al those fancy C++ methods only to return to plain old C-struct.

Ответить
@mikedoeren5960
@mikedoeren5960 - 29.06.2023 22:23

I learned some new things! Nice video.

Ответить
@user-uw7st6vn1z
@user-uw7st6vn1z - 19.05.2023 01:58

why you fold your legs, we want see your nuts

Ответить
@andrewnorris5415
@andrewnorris5415 - 08.12.2022 06:35

Q. Why do you not use it much yourself? Return via. structs and pass by ref?

Ответить
@RogerTannous
@RogerTannous - 06.11.2022 05:21

For std::tuple, what about putting enum ShaderType outside ParseShader(), then doing this::

auto sources = ParseShader("res/shaders/Basic.shader");

unsigned int shader = CreateShader(std::get<ShaderType::VERTEX>(sources), std::get<ShaderType:: FRAGMENT>(sources));

Ответить
@sayedreda3694
@sayedreda3694 - 10.09.2022 14:05

I actually like using the struct more because it's organized and easy to read
and also using vector

using tuple maybe I didn't get so I kinda hate it since i follow the series of openGl

Ответить
@jeanahollings
@jeanahollings - 01.09.2022 22:50

I like hearing you talk about memory stuff and by extension performance. I am very likely to never professionally work in c++ but much more likely to use Java. And while I don't need to know all this, I always do better if I can "get under hood." So while what your saying doesn't definitively apply to Java, it helps me understand why Java does what it does and how it does it and knowing that will help me grok the whole thing better.

Ответить
@vikinggeorge7007
@vikinggeorge7007 - 24.08.2022 13:15

What IF I want to return only one value, but it being one of two types?

Say a string OR a bool?

I use std::variant for this.

Ответить
@solifugus
@solifugus - 01.07.2022 04:11

So... back to old fashion C style structs..... or like in Golang. Yeah... but the {a,b} part makes it C++ nicer.

Ответить
@foomoo1088
@foomoo1088 - 03.06.2022 07:57

I prefer the struct approach as well, self documenting .

Ответить
@svitdimovic4538
@svitdimovic4538 - 21.05.2022 13:03

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.

Ответить
@geamer0079
@geamer0079 - 08.05.2022 00:33

I think the most optimised way would be to return a uintptr_t[] which will contain pointers to any values you want to return

Ответить
@tukunuz
@tukunuz - 18.04.2022 14:38

When struct is returned by value does that mean that it is first created inside the function and then copied when returned to calling function? Is it better to return it as a reference?

Ответить
@jhonlopez27
@jhonlopez27 - 16.04.2022 12:19

Why did you make it so difficult to understand? Couldn't you start from a empty code?

Ответить
@dylangrace1239
@dylangrace1239 - 04.04.2022 15:00

The fast way…
_asm_ (“mov Var1Reg, %%eax;”
“mov Var2Reg, %%ebx;”);
With an ASM function call on the other side to manage placing them where they need to be (or you can just place them there as your return if you know where you want them beforehand) Pretty ugly, but should be very fast because it can keep most of you data types in registers instead of pushing them to the stack

This could be a poor choice of registers because it’s been a little while since I’ve worked with x86 assembly, but you can find that information from plenty of data sheets online if you are actually doing it this way.

Ответить
@dognip
@dognip - 26.02.2022 16:23

Your code is hard to comprehend. I don't like your explanation

Ответить
@webinno
@webinno - 14.02.2022 06:17

I like your way too.

Ответить
@warzonemoments3970
@warzonemoments3970 - 13.02.2022 13:51

One thing I don't get, is in the return he initialized a struct but the struct had no parametised constructor?

Ответить
@MarcinKwiatkowski
@MarcinKwiatkowski - 22.01.2022 00:40

Personally I prefer to use std::tuple<> and using std::tie() to organize return values. Or using auto [a, b, c, d] = get_data();, but unfortunately I'm not allowed to use this, because I'm tied and required to use C++11

Ответить
@hannugupta9711
@hannugupta9711 - 19.12.2021 12:47

I think using struct is better to deal with multiple return values instead of using tuple.

Ответить
@bibbisbibbis
@bibbisbibbis - 27.11.2021 10:45

This is the kind of stuff I'm talking about when I tell people how much I dislike C++.
Why does everything always have to be so unclear to read?

I agree that creating a wrapper struct is the best way to go because it makes everything 100% clear, but why do we have to do that? It's CRAZY when there already exists ways to implement this, but they are so poorly designed that it makes our code less readable.

C# has tuples too, and in C# you can easily access the members just like a pair, but if you don't like to use .first and .second, YOU CAN PROVIDE WHATEVER CUSTOM NAMES YOU WANT.

Creating a custom return type in C# just because you want to return 2 strings with readable names is CRAZY because the functionality is already there and it already provides you with ways to easily make it all 100% clear and readable.

C++ really drives me up the wall...

Ответить
@Ratstail91
@Ratstail91 - 16.11.2021 07:32

Structured binding is now a thing in C++17.

Ответить
@chikipichi5280
@chikipichi5280 - 03.10.2021 20:38

Wow I'm absolutely fucking lost

Ответить
@fmdj
@fmdj - 10.09.2021 23:08

Yes to structs.

Ответить
@cprn.
@cprn. - 08.09.2021 16:58

Setup's nice - your couch ugly, though... It should be black leather with a tiny scared chick on it.

Ответить
@khatharrmalkavian3306
@khatharrmalkavian3306 - 19.08.2021 12:50

Other methods of returning multiple values? Well, you could use a loopback socket....

Don't do that tho. XD

Ответить
@khatharrmalkavian3306
@khatharrmalkavian3306 - 19.08.2021 12:19

Honestly, when I find myself thinking about busting out a tuple I usually just step back and try to figure out where my design went wrong.

Ответить
@bargainbend
@bargainbend - 03.08.2021 22:09

Hey Cherno, love your videos! In last part where you explained struct is the best way to return two values, how did it work by just returning { vs, fs} without allocating it into the heap?

Ответить
@EnderMega
@EnderMega - 28.07.2021 03:25

In python you can automatical... automagically ...

LOL

Ответить
@WizardOfArc
@WizardOfArc - 27.07.2021 06:31

I prefer the strict approach

Ответить
@xeridea
@xeridea - 22.07.2021 20:32

I think for generic stuff, pair and tuple is fine, though tuple syntax slightly clunky, and feel triple and quads should be a standard thing. For stuff used a lot, struct makes sense, but if I have a function called once or twice, I would just use a pair. Pass by reference would be the fastest, and preferred way in performance critical sections, but I don't like cluttering up function call if don't need performance.

Ответить
@alguienmasraro915
@alguienmasraro915 - 17.07.2021 03:53

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();

Ответить
@StevenMartinGuitar
@StevenMartinGuitar - 14.07.2021 10:30

Me: It's pretty annoying that you can only ever return one thing...
Cherno: Yeah so here's about 10 different ways off the dome you can do this 🤯
And that's why I'm here, to learn this stuff!

Ответить
@k-tech2937
@k-tech2937 - 12.07.2021 13:08

IMHO structs are the most natural and reliable way of handling functions with rich returns. Funnily enough that what I see most people who are not this advanced in c++ do. I feel like some very experienced people sometimes do things syntactically more complex just for sake of showing of 😃

Ответить
@sophiarodriguez1861
@sophiarodriguez1861 - 09.07.2021 05:54

Sorry, but the explanation is very messy and incoherent :(

Ответить
@meer_kat5158
@meer_kat5158 - 26.06.2021 21:07

Hi, if std::array allocates memory on stack, how do you return it then?

Ответить
@abbasjradi5001
@abbasjradi5001 - 22.06.2021 14:35

The struct.... Just the struct .... The others are just noo........

Ответить
@zxuiji
@zxuiji - 03.06.2021 12:45

Ironically it's quite simple for a compiler to compile multiple returns down to an anonymous struct and pass the values into the chosen variables so this: "a, b, c = func()" would compile down to roughly "push push push call func mov c +3 mov b +2 mov a +1 pop pop pop" (though in a more assembly friendly then that)

Ответить
@xleonarduz3541
@xleonarduz3541 - 05.05.2021 20:24

After learning python c++ is so annoying XD

Ответить
@joshuatejero5617
@joshuatejero5617 - 14.04.2021 08:35

Hey! , Why an OpenGL video is on C++ Series??, Is this part of C++ Series

Im skipping this one

Ответить
@simond3843
@simond3843 - 21.03.2021 14:51

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!

Ответить
@vbessonow
@vbessonow - 12.03.2021 00:45

Structs

Ответить
@CriticRoshun
@CriticRoshun - 28.02.2021 23:55

My favorite ways are 1. Passing by Reference and 2. Using the struct.
Nice video thanks!

Ответить
@entity5209
@entity5209 - 11.02.2021 00:14

I wonder how he felt like after he stopped recording and realised that tuple is in tuple header file
(not hating he said he doesn't use it as often)

Ответить
@rajshah4376
@rajshah4376 - 26.01.2021 17:36

Why can't we create a static array and return it in a function with a return type of array? Please help. Thanks.

Ответить
@casperes0912
@casperes0912 - 21.01.2021 21:31

Want to return 2 32 bit integer while only returning 1 value?
put the first value in a 64-bit integer and shift it left by 4 bytes so it occupies the 32 most significant bits.
Put the second value in a 64-bit integer so it occupies the 32 least significant bits.
Return the OR of these two.

Now to get your two 32-bit values back just AND the returned value with 0xFFFFFFFF00000000 and 0x00000000FFFFFFFF respectively.

For the record I don't advice doing this

Ответить