Комментарии:
Totally out of my league at this time .
Ответить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).
ОтветитьAl those fancy C++ methods only to return to plain old C-struct.
ОтветитьI learned some new things! Nice video.
Ответитьwhy you fold your legs, we want see your nuts
ОтветитьQ. Why do you not use it much yourself? Return via. structs and pass by ref?
Ответить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));
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
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.
Ответить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.
So... back to old fashion C style structs..... or like in Golang. Yeah... but the {a,b} part makes it C++ nicer.
ОтветитьI prefer the struct approach as well, self documenting .
Ответить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.
ОтветитьI think the most optimised way would be to return a uintptr_t[] which will contain pointers to any values you want to return
Ответить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?
ОтветитьWhy did you make it so difficult to understand? Couldn't you start from a empty code?
Ответить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.
Your code is hard to comprehend. I don't like your explanation
ОтветитьI like your way too.
ОтветитьOne thing I don't get, is in the return he initialized a struct but the struct had no parametised constructor?
Ответить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
ОтветитьI think using struct is better to deal with multiple return values instead of using tuple.
Ответить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...
Structured binding is now a thing in C++17.
ОтветитьWow I'm absolutely fucking lost
ОтветитьYes to structs.
ОтветитьSetup's nice - your couch ugly, though... It should be black leather with a tiny scared chick on it.
ОтветитьOther methods of returning multiple values? Well, you could use a loopback socket....
Don't do that tho. XD
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.
Ответить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?
ОтветитьIn python you can automatical... automagically ...
LOL
I prefer the strict approach
Ответить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.
Ответить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();
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!
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 😃
ОтветитьSorry, but the explanation is very messy and incoherent :(
ОтветитьHi, if std::array allocates memory on stack, how do you return it then?
ОтветитьThe struct.... Just the struct .... The others are just noo........
Ответить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)
ОтветитьAfter learning python c++ is so annoying XD
ОтветитьHey! , Why an OpenGL video is on C++ Series??, Is this part of C++ Series
Im skipping this one
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!
Structs
ОтветитьMy favorite ways are 1. Passing by Reference and 2. Using the struct.
Nice video thanks!
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)
Why can't we create a static array and return it in a function with a return type of array? Please help. Thanks.
Ответить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