[Omp] copy constructor vs. operator= when initializing firstprivate variables

Chunhua Liao liaoch at cs.uh.edu
Wed Jul 12 09:09:07 PDT 2006


On 7/11/06, Richard Henderson <rth at twiddle.net> wrote:
> > 1. Why do we have this specific requirement of using copy constructor here ?
>
> Because that's how a new variable is initialized.  Compare with
>
> class S
> {
>   private:
>     S& operator= (const S&);
>   public:
>     S();
>     S(const S&);
> };
>
> void foo (S *p)
> {
>   S s = *p;
> }
>

Good point, but setting operator= as private might cause problems for
implementing lastprivate, copyprivate etc, where copy assignment
operator is required.

> > 2. Is it legal for a compiler to use operator= to do the same work?
>
> No.  The operator= method will assume that the destination variable
> was previously initialized, which isn't true.

What I meant is something like this:

 void foo (S *p)
 {
   S s;  //default constructor
   s= *p; //operator=
 }

Obviously, this is an indirect way with a little performance penalty .
But I think it is still legal if operator= is not private and default
constructor is available.


More information about the Omp mailing list