i know that when we use the copy form (=) the compiler will pass the right-hand
operand to the copy-constructor of that class.
in addition, the compiler can ignore the use copy constructor of in the initialization of nameless temporary like t in the example.
1 2 3 4 5 6 7 8 9 10 11 12 13 14
struct test {
int i;
test(int v) : i(v) {}
};
int main() {
// the compiler will use copy constructor
test t = 15; // the compiler will use 15 to construct a temp then pass this temp to the copy constructor of test , equivalent to (test t = test(15);)
test t1 = t; // the compiler will pass this object to copy constructor of test
// why not to use the copy constructor MANUALLY
test t3(test(15));
test t4(t);
}
why not to use direct form to use the copy constructor in order to initialize any object , the parameters of a function and the returned temp rather than copy form?
Try compiling your program with and without eliding the copy constructor with the command: -fno-elide-constructors and you'll notice some interesting differences. http://en.cppreference.com/w/cpp/language/copy_elision
thanks keskiverto , gunnerfunner for replying.
i just wanted to know if there are any benefits that i don't know to use copy over direct form like in parameters and returnd values of functions.