Pimpl copying

Rolf Magnus ramagnus at t-online.de
Sun Jul 16 19:15:08 BST 2006


On Friday 14 July 2006 11:55, Reinhold Kainhofer wrote:

> Am Freitag, 14. Juli 2006 11:28 schrieb Lubos Lunak:
> > On Friday 14 July 2006 10:33, André Wöbbeking wrote:
> > > On Thursday 13 July 2006 23:05, Peter Kümmel wrote:
> > > >  void KTempDirTest::testBasic()
> > > >  {
> > > > -       KTempDir dir = KTempDir("test");
> > >
> > > normal ctor and then copy ctor
> >
> >  No. It's just syntactic sugar functionally completely equivalent to the
> > case below with just normal ctor. If any compiler uses copy ctor here
> > it's broken.
>
> Stroustrup's book says that
>    KTempDir dir = KTempDir("test");
> and
>    KTempDir dir("test");
> are equivalent. I.e. the first one should not use ctor and then copy.

I can't believe that. The C++ standard clearly says something else.

> I had a similar discussion with Mark a while ago (we were discussion which
> one was more efficient. Turned out they should be the same)

Well, eliding the copy constructor is an allowed optimization, but not 
required. Most compilers nowadays do it, AFAIK. However, a copy constructor 
still _must_ be available, even if the call is optimized away. That's because 
C++ requires that the correctness of code doesn't depend on optimization 
behavior. Consider this:

class Test
{
public:
    Test(int x) { }
private:
    Test(const Test&);
};

int main()
{
    Test t(3);             // will compile
    Test t2 = Test(3); // won't compile, even if copy constructor is not used
}

Any compiler that accepts the above code is broken.




More information about the kde-core-devel mailing list