2

I googled about copy initialization and found out that whenever we write T a = b; ,copy initialisation takes place. It was also mentioned that copy initialization also takes when we pass arguments by value in a function call.

I wanted to know that whenever we pass arguments to a function, is the " = " operator used by the compiler to copy initialize the parameters of the function? I know that "=" is not needed in syntax of the function call but does the copy initialization itself use "=" during function call by value?

For example , In the code given below :

#include<iostream>
using namespace std;
void fun(T1 x, T2 y){
    // some code
}
int main(){
   T1 a;
   T2 b;
   solve(a,b);
}

Does initialization of parameters take place as T1 x = a ; and T2 y = b ; ? Is the "=" operator involved during the copy initialization of parameters in the function?

  • 1
    Not sure if this would be already the whole answer: in `T1 x = a;` no `operator=` is used. It is initialization not assignment. `operator=` is only used for assignment. If thats your misunderstanding then there are duplicates – 463035818_is_not_an_ai May 10 '22 at 09:39
  • Are you asking if copy-*assignment* is performed anywhere here? Because the answer is no if that is the case. – WhozCraig May 10 '22 at 09:39
  • @WhozCraig No , I am talking about copy-initialization. "=" is not used in the sense of assignment in copy-initialization . https://en.cppreference.com/w/cpp/language/copy_initialization – Divyanshu Dwivedi May 10 '22 at 09:41
  • @AnoopRana The answer in the linked question does not answer clearly whether "=" comes into the picture during copy-initialization in case of argument passing in a function call. – Divyanshu Dwivedi May 10 '22 at 09:47
  • @DivyanshuDwivedi See in my answer below, no copy assignment `operator=` is used when passing argument. In C++ initialization and assignment are not the same. Argument passing to parameter happens using copy initialization and not copy assignment. – Jason May 10 '22 at 09:48
  • @AnoopRana I am not saying that "=" is used for copy-assignment. For example, ` T a = b ` is copy-initialization using "=". In the same way, I am asking whether parameters are also initialized in the same way ( i.e T1 x = a; ) or not ? – Divyanshu Dwivedi May 10 '22 at 09:52
  • @DivyanshuDwivedi I'm well aware; just making sure *you* were as well. And to answer your question, copy initialization doesn't "use" `=` for anything besides permissible syntax. `T x = a;` is synonymous with `T x(a);` in the context of your question – WhozCraig May 10 '22 at 09:55
  • @DivyanshuDwivedi `T1 x = a;` is called *"the = form of a brace-or-equal-initializer"* of copy initialization. While argument passing is also copy initialization. See the quoted statement in my answer below. The same is mentioned there. When you pass argument say `a` and `b` to parameter say `x` and `y` then it is if you wrote `T1 x =a;` and `T2 y = b;`. – Jason May 10 '22 at 09:57
  • @WhozCraig Do you agree with the above comment by Anoop Rana ? – Divyanshu Dwivedi May 10 '22 at 10:02
  • @DivyanshuDwivedi See the third point listed [here](https://en.cppreference.com/w/cpp/language/copy_initialization) which says *"Copy initialization is performed in the following situations: when passing an argument to a function by value `f(other)`"*. When passing the argument to the function in your example, the observed behavior is the same as if you were to write `T1 x = a;` and `T2 y =b;` . More explanation is given in the given link. Which explains what effects does copy initialization have. You can confirm this using this [demo](https://onlinegdb.com/2SfyNwp5u). – Jason May 10 '22 at 10:08
  • 1
    @DivyanshuDwivedi You can confirm this using this [demo](https://onlinegdb.com/2SfyNwp5u). See the effect is the same when using `=` form or passing argument. Also i recommend learning C++ using a [good C++ book](https://stackoverflow.com/questions/388242/the-definitive-c-book-guide-and-list). – Jason May 10 '22 at 10:11
  • @DivyanshuDwivedi I do, and it is fairly trivial [to demo](https://godbolt.org/z/xdYfMTEqc). – WhozCraig May 10 '22 at 10:12
  • I agree that this is confusing for people new to C++. But it is what it is, and will likely never change. – JHBonarius May 10 '22 at 10:37

2 Answers2

2

Argument passing to the parameter of a function happens using copy initialization which is different than using copy assignment operator=. Note that initialization and assignment are two different things in C++. In particular, passing argument happens using "copy initialization" and not "copy assignment".

From decl.init.general#14:

The initialization that occurs in the = form of a brace-or-equal-initializer or condition ([stmt.select]), as well as in argument passing, function return, throwing an exception ([except.throw]), handling an exception ([except.handle]), and aggregate member initialization ([dcl.init.aggr]), is called copy-initialization.

(end quote)

Similarly, from copy initialization:

Copy initialization is performed in the following situations:

    1. when passing an argument to a function by value

(end quote)

Also, note that the order of the copy initialization of the parameter from the arguments is unspecified.


Does initialization of parameters take place as T1 x = a ; and T2 y = b ; ?

In your example, when you passed arguments a and b to parameters x and y, the observed behavior would be as if we wrote T1 x = a; and T2 y = b;. You can verify this using this demo. Note again that the order of copy is unspecified.

Jason
  • 36,170
  • 5
  • 26
  • 60
0

In initializing the function parameters, T1::operator= is not called. Instead T1::T1( const T1 &) is called.

The operator= could only be called if the argument were first constructed, using T1::T1(), which might not even exist.

You could create all these functions and put log-output in them to see this behavior in action.

Mark Roberts
  • 138
  • 7