BinaryByter
but if possible, use cont over not-const
Mark
rarely
olli
Difference b/w 1. func( int a, int b, int c) 2. func( int& a, int& b, int& c) 3. func( const int& a,const int& b,const int& c) Which one is best to use in C++ ?
2) only if func is allowed to modify a, b and c. This also means you cannot pass temporaries / prvalues 1) and 3) are somewhat similar, I'd go with 1) in this case (the address/reference might be larger than the value itself)
abhi3700
When it is about performance, i think we should not choose 1), as it creates a copy.
abhi3700
For me actually, 3) makes more appropriate.
olli
When it is about performance, i think we should not choose 1), as it creates a copy.
this is true for object that are expensive to copy. However, a reference is like a pointer. On 64 bit systems the pointer might be 8 Byte whereas the integer itself might only be 4 byte..
abhi3700
So, what do you suggest 2) as the best option.
abhi3700
?
Anonymous
the best one is 3), in this case how olli said it is useless
olli
consider this extern void f0(int,int,int); extern void f1(const int&,const int&,const int&); calling f0(1,2,3) generates mov edx, 3 mov esi, 2 mov edi, 1 call f0(int, int, int) calling f1(1,2,3) generates lea rdx, [rsp+12] lea rsi, [rsp+8] mov DWORD PTR [rsp+12], 3 lea rdi, [rsp+4] mov DWORD PTR [rsp+8], 2 mov DWORD PTR [rsp+4], 1 call f1(int const&, int const&, int const&)
Anonymous
it depends on how compiler implements lvalue reference
abhi3700
👍👍
abhi3700
In int type, 1) is best.
olli
If func should not modify the paramters, use the first approach
abhi3700
But for object type arguments, 3) is best
olli
for non primitive types 3) might be a better choice (e.g. string, vector, ...)
abhi3700
Primitive types- e.g.. ??
olli
to complete the example above int a = 1, b = 2, c = 3; f0(a,b,c); generates mov edx, 3 mov esi, 2 mov edi, 1 jmp f0(int, int, int) int a = 1, b = 2, c = 3; f1(a,b,c); generates lea rdx, [rsp+12] lea rsi, [rsp+8] mov DWORD PTR [rsp+4], 1 lea rdi, [rsp+4] mov DWORD PTR [rsp+8], 2 mov DWORD PTR [rsp+12], 3 call f1(int const&, int const&, int const&)
olli
Primitive types- e.g.. ??
int, float, long, char, boolean, ..
olli
or in other words, it doesn't really matter whether you take a reference to something or create a trivial copy if it does fit in a register
abhi3700
But, still I am not sure I have mastered it entirely.
abhi3700
What sources I can go through in order to master it.
abhi3700
@ollirz can u suggest some?
olli
@ollirz can u suggest some?
I think you have a decent understanding! If you are unsure either use const & or ask us (const & does not depend on the type and should always be equally fast) Otherwise maybe read [1] and [2], Item 41 from Effective Modern C++ might be also a good read [1] https://stackoverflow.com/questions/373419/whats-the-difference-between-passing-by-reference-vs-passing-by-value [2] https://stackoverflow.com/questions/40185665/performance-cost-of-passing-by-value-vs-by-reference-or-by-pointer
BinaryByter
they might be
they are not useful in terms of efficiency*
Mark
https://0x0.st/sUVH.png
Mark
...
olli
Ohk... thanks!! 👍👍👌👌
sure, you're welcome :)
abhi3700
Anyone here has ever attended cppcon held every year?
Saurabh
If anyone has good stl c++ notes pdf please share with me
Saurabh
If you have pdf please share me personally
klimi
#freeprogrammingbooks
Saurabh
Yes
Mark
just google for some
Mark
it's not hard
klimi
I don't have mood for this
klimi
/saved
klimi
Read pinned msg
klimi
Everything you need
Mark
@rebane2001 don't click on that button
Mark
ur not a human
Mark
:^)
Rebane
why they have google recaptcha on c server
Avamander
Is that button supposed to work?
BinaryByter
it did work
BinaryByter
you have to click on it in order to write
Avamander
Okay, good UX right there.
Avamander
:D
BinaryByter
not mine 🤷‍♂
BinaryByter
BinaryByter
SEXISM
Mark
Rebane
We should delete telegram from internet
BinaryByter
i pronounce gif like 'yiff'.
BinaryByter
jk, i'm not from northern germany
Mark
>yiff not yiff you expected to be
Rebane
I pronounce it as yiff because that pisses mark off
BinaryByter
@ollirz ja jetzt jönnen wir uns doch eine reaktjionsjif
BinaryByter
only he will understand my allusion to northern germans :(
BinaryByter
maybe he will
BinaryByter
im too tired to be cool
Mark
>yiff not yiff you expected to be
probably nobody got the reference or just too frightened to say anything lmao
Mark
great
BinaryByter
im always too tired no matter how mucd i sleep
Rebane
I'm not cool either way
BinaryByter
ima sleep
Mark
aight gn
BinaryByter
class test in maths tomorrow
BinaryByter
about quadratic formulaes