+ 1
Solve this issue
This is simple c++ program, whenever it is time to call default copy constructor, instead I want to call user defined copy constructor, but it is showing error here, please check. And it works perfectly fine if I seperate declaration and function Call (see code), please give reason why it is not working https://code.sololearn.com/c4p4NNUTSKWC/?ref=app
13 Answers
+ 6
rajshekhar y dodamani just an addition to Ipang 's answer
when you do
A a = fun();
it is actually passing the return value of `fun()` into the constructor of A, that is, A::A(fun())
Now in the constructor, you are asking for the type `A&`, which accepts only lvalues, basically, values which are bound to a variable/object field. Which means that you *cannot* pass temporary values to it. Every value is 'temporary' as long as it is not bound to a variable or object. When you return a value from fun(), it is temporary as no variable or object owns it. And for this reason, passing it to the constructor of `A` gives error. Taking `const A&` type removes this restriction, and that is why the code works.
My explanation of lvalues is pretty naive and they can be useful at times. See here for lvalues and rvalues
https://www.internalpointers.com/post/understanding-meaning-lvalues-and-rvalues-c
https://docs.microsoft.com/en-us/cpp/cpp/lvalues-and-rvalues-visual-cpp?view=msvc-160
+ 6
Your code compiles in SL playground because of "copy elision" (https://en.cppreference.com/w/cpp/language/copy_elision) which is when your compiler decides to ignore your copy/move constructors and construct the object directly where it was supposed to be copied/moved to. If you add a print in your copy constructor you'll see that it doesn't get called. The type of copy elision happening in your code is optional to compilers. Your code does not compile in programiz because the compiler simply decides not to copy elide (different compilers/versions), so it tries to find a suitable constructor and causes the error answered by Ipang and XXX
+ 3
Ipang
yes, as they are not bound to anything from which you can access them. I might be wrong, but I like to think of rvalues as 'values which will be deleted when the statement is over'. For example,
func("str");
Here the string was not bounded to a variable and thus when the statement is over, it will be deleted, and hence it is a rvalue. lvalues are mostly variables or object fields or members of arrays or vectors or even dereferenced pointers, anything that is held onto (I think).
+ 2
Yeah it is running fine here, but on programiz.com online c++ compiler, it is giving error, please run it on that platform and check it.
+ 2
I see 👌
Just change line 10 by adding `const` for argument <a>.
A (const A& a) { // A(A &a) {
+ 2
Ipang hmm now it worked, but why it so
+ 2
Adding `const` is to comply the error message
"cpp:22:12: error: cannot bind non-const lvalue reference of type 'A&' to an rvalue of type 'A'
"
22 | A a=fun();
+ 2
XXX
Are literals rvalues? I have been quite confused about definition of lvalues & rvalues
+ 2
This is copy elision I suppose:
https://stackoverflow.com/questions/12953127/what-are-copy-elision-and-return-value-optimization
+ 2
+ 1
It is not showing error, or did you change the code?
+ 1
XXX
Thanks very much, one more doubt cleared 🙏
+ 1
Thanks for additional knowledge jtrh and CarrieForle 🙏