D
dave_dp
Hi, I have just started learning C++ language..
I've read much even tried to understand the way standard says but still
can't get the grasp of that concept.
When parameters are passed/returned by value temporaries are
created?(I'm not touching yet the cases where standard allows
optimizations from the side of implementations to avoid copying)
If so, please quote part of the standard that says that.
Assuming it is true, I can imagine two cases such as:
struct X {
};
void foo(X b);
....
int main(int argc, char* argv[]) {
X a;
foo(a); // first case
foo(X()); // second one
}
In the first case temporary variable is created that is *copy*
constructed with 'a' object and upon function call 'b' object is itself
copy constructed with that temporary variable.. am I doing right
assumtions iff (When parameters are passed/returned by value
temporaries are created) ?
So copy .ctor is used two times here ?
In the second case X() expression that is rvalue of type X.. I'm not
sure as well about the terminology to use here... X() expression is not
temporary object right? it is just a rvalue(that is value initialized)
and temporary object of type X is *copy* constructed with that rvalue
and then 'b' object is also copy constructed from that temporary...
Same holds true for returning objects ?
Now, if everything holds true, implementators can avoid two phase
copying, like in first example instead to copy construct temporary with
'a' object and then to copy construct 'b' object with that temporary,
'b' object is directly copy constructed with the 'a' object?
And in second case X() doesn't create temporary but directly copy
constructs 'b' ?
TIA.
I've read much even tried to understand the way standard says but still
can't get the grasp of that concept.
When parameters are passed/returned by value temporaries are
created?(I'm not touching yet the cases where standard allows
optimizations from the side of implementations to avoid copying)
If so, please quote part of the standard that says that.
Assuming it is true, I can imagine two cases such as:
struct X {
};
void foo(X b);
....
int main(int argc, char* argv[]) {
X a;
foo(a); // first case
foo(X()); // second one
}
In the first case temporary variable is created that is *copy*
constructed with 'a' object and upon function call 'b' object is itself
copy constructed with that temporary variable.. am I doing right
assumtions iff (When parameters are passed/returned by value
temporaries are created) ?
So copy .ctor is used two times here ?
In the second case X() expression that is rvalue of type X.. I'm not
sure as well about the terminology to use here... X() expression is not
temporary object right? it is just a rvalue(that is value initialized)
and temporary object of type X is *copy* constructed with that rvalue
and then 'b' object is also copy constructed from that temporary...
Same holds true for returning objects ?
Now, if everything holds true, implementators can avoid two phase
copying, like in first example instead to copy construct temporary with
'a' object and then to copy construct 'b' object with that temporary,
'b' object is directly copy constructed with the 'a' object?
And in second case X() doesn't create temporary but directly copy
constructs 'b' ?
TIA.