Topic: init () or =


Author: jpotter@falcon.lhup.edu (John E. Potter)
Date: 1996/05/24
Raw View
class NoCopy {
 public :
  NoCopy (int v) : x(v) { }
 private :
  int x;
  NoCopy (NoCopy const&);
 };
void f () {
 NoCopy a(5);      // ok
 NoCopy b(a);      // error copy ctor inaccessible
 NoCopy c = a;     // error same as c(a)
 NoCopy d = 7;     // error ?????
 }
My read of 8.5[dcl.init]/11 is that the questioned line is interpreted as
 NoCopy d(NoCopy(7));
12.2[class.temporary]/1 allows avoiding creation of the temporary only when
the copy ctor is accessible.

None of my compilers give a diagnostic on this line.
Have I missed something?

John
---
[ comp.std.c++ is moderated.  To submit articles: try just posting with      ]
[ your news-reader.  If that fails, use mailto:std-c++@ncar.ucar.edu         ]
[ FAQ:      http://reality.sgi.com/employees/austern_mti/std-c++/faq.html    ]
[ Policy:   http://reality.sgi.com/employees/austern_mti/std-c++/policy.html ]
[ Comments? mailto:std-c++-request@ncar.ucar.edu                             ]





Author: u9406188@muss.CIS.McMaster.CA (R.W. Johnstone)
Date: 1996/05/24
Raw View
In article <4o1pk2$smk@jake.esu.edu>,
John E. Potter <jpotter@falcon.lhup.edu> wrote:
>void f () {
> NoCopy a(5);      // ok
> NoCopy b(a);      // error copy ctor inaccessible
> NoCopy c = a;     // error same as c(a)
> NoCopy d = 7;     // error ?????
> }

What your complier is probably doning is creating a temporary variable
with the int ctor and then trying to copy that termporary variable to the
newly created variable "d".  Which, because the copy ctor is inaccessible,
makes it choke.

I'm not complete sure, but the two methods of initialization do not mean
the same thing.  While the difference between the assignment and ctor in
initilization are so subtly different as to be inconsequential for simple
data types, it plays a big difference for more complex types.


[ comp.std.c++ is moderated.  To submit articles: try just posting with      ]
[ your news-reader.  If that fails, use mailto:std-c++@ncar.ucar.edu         ]
[ FAQ:      http://reality.sgi.com/employees/austern_mti/std-c++/faq.html    ]
[ Policy:   http://reality.sgi.com/employees/austern_mti/std-c++/policy.html ]
[ Comments? mailto:std-c++-request@ncar.ucar.edu                             ]