C++Talk.NET Forum Index C++Talk.NET
C++ language newsgroups
 
Archives   FAQFAQ   SearchSearch   MemberlistMemberlist   UsergroupsUsergroups   RegisterRegister 
 ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

What is a copy constructor, exactly?
Goto page 1, 2  Next
 
Post new topic   Reply to topic    C++Talk.NET Forum Index -> C++ Language (Moderated)
View previous topic :: View next topic  
Author Message
Alan McKenney
Guest





PostPosted: Tue Dec 20, 2005 12:34 am    Post subject: What is a copy constructor, exactly? Reply with quote



The latest thread on RVO brought to mind the question:

What exactly makes a constructor qualify as a "copy constructor",
and eligible for all the special priviledges reserved to
copy constructors, e.g., RVO?


Obviously,

A( const A & )

is a copy constructor.

How about:

A( A & ) // no const -- familiar to fans of auto_ptr.

On the other hand, I'm pretty sure

A( A )

can't be a copy constructor, if only because you would need a
copy constructor to copy the argument, and *that* copy constructor
would need a copy constructor, and so on.

(For that matter, is there any situation where such a pathological
"constructor" would be invoked?)

Or how about:

class A : public B { A( const B & ); ... };

Just how far can you go from the canonical A( const A & )
and still qualify as a copy constructor?

(Inquiring minds want to know, and all that....)

-- Alan McKenney


[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]

Back to top
Carlos Moreno
Guest





PostPosted: Tue Dec 20, 2005 10:51 am    Post subject: Re: What is a copy constructor, exactly? Reply with quote



Alan McKenney wrote:

Quote:
What exactly makes a constructor qualify as a "copy constructor",
and eligible for all the special priviledges reserved to
copy constructors, e.g., RVO?


Obviously,

A( const A & )

is a copy constructor.

How about:

A( A & ) // no const -- familiar to fans of auto_ptr.

On the other hand, I'm pretty sure

A( A )

can't be a copy constructor,

I'm not sure if the standard does address this specific issue,
but from what I remember when reading/learning about C++, this
*is* a copy-constructor; one that is incorrectly implemented
(unconditionally incorrect), because, as you pointed out, it
leads to an infinite loop.

A copy constructor is a constructor that receives, as argument,
an object of the same class -- the above examples are simply
reflecting different ways of passing the argument: if you
pass it by value, then the implementation is unconditionally
incorrect. The other two are valid, even if most of the time
we would prefer the const version.

Quote:
(For that matter, is there any situation where such a pathological
"constructor" would be invoked?)

If you provide it, then any time that the program needs to
create a copy of an object of that class, then the constructor
would have to be invoked, leading to mayhem/havoc/chaos.

Carlos
--

[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]


Back to top
Gerhard Menzl
Guest





PostPosted: Tue Dec 20, 2005 10:51 am    Post subject: Re: What is a copy constructor, exactly? Reply with quote



Alan McKenney wrote:

Quote:
What exactly makes a constructor qualify as a "copy constructor",
and eligible for all the special priviledges reserved to
copy constructors, e.g., RVO?


Obviously,

A( const A & )

is a copy constructor.

How about:

A( A & ) // no const -- familiar to fans of auto_ptr.

On the other hand, I'm pretty sure

A( A )

can't be a copy constructor, if only because you would need a
copy constructor to copy the argument, and *that* copy constructor
would need a copy constructor, and so on.

(For that matter, is there any situation where such a pathological
"constructor" would be invoked?)

Or how about:

class A : public B { A( const B & ); ... };

Just how far can you go from the canonical A( const A & )
and still qualify as a copy constructor?

The C++ Standard is quite clear about this. 12.8/2 says:

"A non-template constructor for class X is a copy constructor if its
first parameter is of type X&, const X&, volatile X& or const volatile
X&, and either there are no other parameters or else all other
parameters have default arguments".

This leaves room for some pretty exotic looking copy constructors, such as

A (A volatile &, double = 3.141592);


--
Gerhard Menzl

#dogma int main ()

Humans may reply by replacing the thermal post part of my e-mail address
with "kapsch" and the top level domain part with "net".

[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]


Back to top
Rob
Guest





PostPosted: Tue Dec 20, 2005 11:14 am    Post subject: Re: What is a copy constructor, exactly? Reply with quote

Alan McKenney wrote:

Quote:
The latest thread on RVO brought to mind the question:

What exactly makes a constructor qualify as a "copy constructor",
and eligible for all the special priviledges reserved to
copy constructors, e.g., RVO?

A copy constructor is invoked in creating a copy of an object.

For class A, the copy constructor has the form

A(<cv qualifier> A <cv qualifier> &);

where cv-qualifier is a mix of const and volatile keywords.


Quote:


Obviously,

A( const A & )

is a copy constructor.

How about:

A( A & ) // no const -- familiar to fans of auto_ptr.

On the other hand, I'm pretty sure

A( A )

can't be a copy constructor, if only because you would need a
copy constructor to copy the argument, and that copy constructor
would need a copy constructor, and so on.

Such a constructor would be infinitely recursive, so is disallowed.

Quote:

(For that matter, is there any situation where such a pathological
"constructor" would be invoked?)

It is not allowed, so ... no.

Quote:

Or how about:

class A : public B { A( const B & ); ... };


This would not qualify as a copy constructor.

[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]


Back to top
peter steiner
Guest





PostPosted: Tue Dec 20, 2005 11:16 am    Post subject: Re: What is a copy constructor, exactly? Reply with quote

Alan McKenney wrote:
Quote:
The latest thread on RVO brought to mind the question:

What exactly makes a constructor qualify as a "copy constructor",
and eligible for all the special priviledges reserved to
copy constructors, e.g., RVO?

the standard is explicit about the definition of a copy constructor:
(12.1.10)

'A copy constructor for a class X is a constructor with a first
parameter of type X& or type const X&.'

or more specifically: (12.8.2)

'A non-template constructor for class X is a copy constructor if its
first parameter is of type X&, const X&, volatile X& or const volatile
X& and either there are no other parameters or else all other
parameters have default arguments.'

Quote:
On the other hand, I'm pretty sure

A( A )

can't be a copy constructor, if only because you would need a
copy constructor to copy the argument, and *that* copy constructor
would need a copy constructor, and so on.

(For that matter, is there any situation where such a pathological
"constructor" would be invoked?)

the definition of the above constructor is ill-formed and should not
compile in non-template code and not be instantiated otherwise.
(12.8.3)

-- peter


[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]


Back to top
peter steiner
Guest





PostPosted: Tue Dec 20, 2005 11:17 am    Post subject: Re: What is a copy constructor, exactly? Reply with quote

Alan McKenney wrote:
Quote:
The latest thread on RVO brought to mind the question:

What exactly makes a constructor qualify as a "copy constructor",
and eligible for all the special priviledges reserved to
copy constructors, e.g., RVO?

what do you mean with RVO being a special priviledge being reserved for
copy constructors?

return value optimization applies to all functions that have to return
a value. the semantics of a function to be optimized do not matter in
that regard, if the optimization is possible depends purely on the
applicability of the compilers optimization algorithm to the function
body.

perhaps you are confusing the use of copy constructors in that past
thread's examples to analyze the performance of RVO (ie. if copying
takes place and how often) with the bigger idea of RVO.

-- peter


[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]


Back to top
Carlos Moreno
Guest





PostPosted: Tue Dec 20, 2005 2:31 pm    Post subject: Re: What is a copy constructor, exactly? Reply with quote

peter steiner wrote:
Quote:
Alan McKenney wrote:

The latest thread on RVO brought to mind the question:

What exactly makes a constructor qualify as a "copy constructor",
and eligible for all the special priviledges reserved to
copy constructors, e.g., RVO?


what do you mean with RVO being a special priviledge being reserved for
copy constructors?

return value optimization applies to all functions that have to return
a value. the semantics of a function to be optimized do not matter in
that regard

There is a difference, in that the compiler is allowed to
optimize away unnecessary copying of objects even if the
contructor and destructor do have observable side-effects;
this is not the case with regular functions -- as discussed
recently in another thread, the compiler is not allowed to
optimize away the call to f() in the following fragment of
code:

int g()
{
return 0 * f();
}

(unless it has access to the definition of f() and verifies
that f() has no observable side-effects)

With the copy-constructor, even if the compiler sees that
it has side-effects (or the destructor), the compiler can
optimize away copies of objects if it decides it is a good
idea.

Carlos
--

[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]


Back to top
Francis Glassborow
Guest





PostPosted: Tue Dec 20, 2005 2:32 pm    Post subject: Re: What is a copy constructor, exactly? Reply with quote

In article <1135073401.692873.296540 (AT) g44g2000cwa (DOT) googlegroups.com>,
peter steiner <pnsteiner (AT) gmail (DOT) com> writes
Quote:
the standard is explicit about the definition of a copy constructor:
(12.1.10)

'A copy constructor for a class X is a constructor with a first
parameter of type X& or type const X&.'

or more specifically: (12.8.2)

'A non-template constructor for class X is a copy constructor if its
first parameter is of type X&, const X&, volatile X& or const volatile
X& and either there are no other parameters or else all other
parameters have default arguments.'


And when I looked for the same information I noted that the above are
not entirely in agreement.

Originally the pre-standard draft was unclear on the subject of what
constituted a copy ctor. I was one of those who raised the issue because
I needed to know what would suppress the implicit generation of a copy
ctor. When this point was grasped the decision was made that the
volatile versions should also class as copy ctors even though no one
could see any immediate use for such a ctor.

It seems that we did not spot that the first of the above needed
amending.

--
Francis Glassborow ACCU
Author of 'You Can Do It!' see http://www.spellen.org/youcandoit
For project ideas and contributions: http://www.spellen.org/youcandoit/projects


[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]


Back to top
peter steiner
Guest





PostPosted: Tue Dec 20, 2005 7:00 pm    Post subject: Re: What is a copy constructor, exactly? Reply with quote

Carlos Moreno wrote:
Quote:
peter steiner wrote:
Alan McKenney wrote:

The latest thread on RVO brought to mind the question:

What exactly makes a constructor qualify as a "copy constructor",
and eligible for all the special priviledges reserved to
copy constructors, e.g., RVO?


what do you mean with RVO being a special priviledge being reserved for
copy constructors?

return value optimization applies to all functions that have to return
a value. the semantics of a function to be optimized do not matter in
that regard

There is a difference, in that the compiler is allowed to
optimize away unnecessary copying of objects even if the
contructor and destructor do have observable side-effects;
this is not the case with regular functions -- as discussed
recently in another thread, the compiler is not allowed to
optimize away the call to f() in the following fragment of
code:

int g()
{
return 0 * f();
}

(unless it has access to the definition of f() and verifies
that f() has no observable side-effects)

With the copy-constructor, even if the compiler sees that
it has side-effects (or the destructor), the compiler can
optimize away copies of objects if it decides it is a good
idea.

oh, now i understand the idea of the original poster. i was thinking of
RVO inside the copy constructor...

-- peter


[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]


Back to top
peter steiner
Guest





PostPosted: Tue Dec 20, 2005 7:00 pm    Post subject: Re: What is a copy constructor, exactly? Reply with quote

Francis Glassborow wrote:
Quote:
In article <1135073401.692873.296540 (AT) g44g2000cwa (DOT) googlegroups.com>,
peter steiner <pnsteiner (AT) gmail (DOT) com> writes
the standard is explicit about the definition of a copy constructor:
(12.1.10)

'A copy constructor for a class X is a constructor with a first
parameter of type X& or type const X&.'

or more specifically: (12.8.2)

'A non-template constructor for class X is a copy constructor if its
first parameter is of type X&, const X&, volatile X& or const volatile
X& and either there are no other parameters or else all other
parameters have default arguments.'


And when I looked for the same information I noted that the above are
not entirely in agreement.

Originally the pre-standard draft was unclear on the subject of what
constituted a copy ctor. I was one of those who raised the issue because
I needed to know what would suppress the implicit generation of a copy
ctor. When this point was grasped the decision was made that the
volatile versions should also class as copy ctors even though no one
could see any immediate use for such a ctor.

It seems that we did not spot that the first of the above needed
amending.

yes, there is a little inconsistency here, though i have to note that
this is quoted from the C++98 version, as i do not have the C++03
standard at hand at the moment. i will have a look at the current
version at home and forward the problem to comp.std.c++ if it has not
been corrected in the current version.


[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]


Back to top
Alan McKenney
Guest





PostPosted: Fri Dec 23, 2005 6:43 pm    Post subject: Re: What is a copy constructor, exactly? Reply with quote


peter steiner wrote:

<snip>

Quote:
oh, now i understand the idea of the original poster. i was thinking of
RVO inside the copy constructor...

-- peter

This may be a dumb question, but:

what do you mean by "RVO inside the copy constructor"?

Given that copy constructors don't have return values,
I don't understand how you could have a Return Value Optimization.

(Inquiring minds want to know....)

-- Alan McKenney


[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]


Back to top
peter steiner
Guest





PostPosted: Tue Dec 27, 2005 1:02 pm    Post subject: Re: What is a copy constructor, exactly? Reply with quote

Alan McKenney wrote:
Quote:
peter steiner wrote:

snip

oh, now i understand the idea of the original poster. i was thinking of
RVO inside the copy constructor...

-- peter

This may be a dumb question, but:

what do you mean by "RVO inside the copy constructor"?

Given that copy constructors don't have return values,
I don't understand how you could have a Return Value Optimization.

well, RVO is nothing but the option for the compiler to optimize
temporary objects away. the whole strategy applies to code in general,
not just return values.

i did misuse the term RVO but i don't see any difference between:

int foo()
{
return 42;
}

and:

void bar(int& out)
{
out = 42;
}

and:

void Foo::bar()
{
this->baz = 42;
}

in the context of temporary object optimization with the small
exception that in the first two cases the 'returned' object may
themselves be temporaries that could be optimized away too (which is
obviously not possible in the third case). 'return values' just modify
object values somewhere in the program memory, at least that is how the
compiler sees it when it starts to apply any optimizations.

-- peter


[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]


Back to top
John Potter
Guest





PostPosted: Tue Dec 27, 2005 6:36 pm    Post subject: Re: What is a copy constructor, exactly? Reply with quote

On 27 Dec 2005 08:02:14 -0500, "peter steiner" <pnsteiner (AT) gmail (DOT) com>
wrote:

Quote:
well, RVO is nothing but the option for the compiler to optimize
temporary objects away. the whole strategy applies to code in general,
not just return values.

i did misuse the term RVO but i don't see any difference between:

The big difference is between construction and assignment.

Quote:
int foo()
{
return 42;
}

Consturction. That constructs an int with value 42. The possible
optimization is to not construct an int with value 42 and then copy it
to the returned int, but just construct the returned int with value 42.
This is not RVO.

Quote:
and:

void bar(int& out)
{
out = 42;
}

Assignment. That assigns 42 to an existing int object. There is no
return value. There is no copy construction to remove.

Quote:
and:

void Foo::bar()
{
this->baz = 42;
}

Assignment. As above, there is no copy construction to remove.

Quote:
in the context of temporary object optimization with the small
exception that in the first two cases the 'returned' object may
themselves be temporaries that could be optimized away too (which is
obviously not possible in the third case). 'return values' just modify
object values somewhere in the program memory, at least that is how the
compiler sees it when it starts to apply any optimizations.

Obvious confusion. There is no return value except in the first case.
Let's look at what RVO is. This involves removal of a temporary for
returning a value to be used for initialization of an object in the
caller. Since fundamental types are normally returned in registers, it
is unlikely to apply to them. Note that inlining has nothing to do with
RVO.

struct S {
int s;
S (int s = 0) : s(s) { }
S (S const& o) : s(o.s) { }
S& operator+= (S const& rhs) { s += rhs.s; return *this; }
};
S f () { return S(42); }
int main () {
S s(f());
}

With no optimization, we have: construct an S from 42, construct the
return value of f from that using the copy constructor, construct
main::s from that using the copy constructor. The first simple
optimization which you talk about above is to remove the first object
and construct the return value of f from 42. That is not RVO. With
RVO, the return value is removed and main::s is constructed from 42 with
no temporaries. The way that this is done without inlining is to use a
returning convention which allows it. The function is rewritten as:

void f (void* rv) { new (rv) S(42); }

In main, the properly alligned raw memory is allocated and its address
is passed to the function. The placement new in f direct constructs
main::s with no temporaries. This is RVO.

There has also been some historic confusion on the terms RVO and NRVO
which have both been used to describe another optimization.

S operator+ (S const& lhs, S const& rhs) {
S tmp(lhs);
tmp += rhs;
return tmp;
}

The NRVO removes the local named variable tmp by considering it another
name for the returned value. Combined with the above RVO for operator+
we can again get no temporaries when adding another line to main.

S s2(s + s);

The alligned space for s2 is provided in main and its address is passed
to the rewritten operator+ void* parameter. The first line is replaced
by placement new copy construction of main::s2 from lhs. The second
line modifies main::s2 my adding rhs to it. The return goes away.

There is more to RVO and NRVO than simple copy constructor removal. The
first works across function calls in different units and the second
removes a named variable from the program. Special allowance for these
actions in addition to the simple copy removal was added to the standard
with much heated debate.

John

[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]


Back to top
kanze
Guest





PostPosted: Tue Dec 27, 2005 6:42 pm    Post subject: Re: What is a copy constructor, exactly? Reply with quote

Francis Glassborow wrote:
Quote:
In article
[email]1135073401.692873.296540 (AT) g44g2000cwa (DOT) googlegroups.com[/email]>, peter
steiner <pnsteiner (AT) gmail (DOT) com> writes
the standard is explicit about the definition of a copy
constructor: (12.1.10)

'A copy constructor for a class X is a constructor with a
first parameter of type X& or type const X&.'

or more specifically: (12.8.2)

'A non-template constructor for class X is a copy constructor
if its first parameter is of type X&, const X&, volatile X&
or const volatile X& and either there are no other parameters
or else all other parameters have default arguments.'

And when I looked for the same information I noted that the
above are not entirely in agreement.

Originally the pre-standard draft was unclear on the subject
of what constituted a copy ctor. I was one of those who
raised the issue because I needed to know what would suppress
the implicit generation of a copy ctor. When this point was
grasped the decision was made that the volatile versions
should also class as copy ctors even though no one could see
any immediate use for such a ctor.

It seems that we did not spot that the first of the above needed
amending.

The first also neglects to mention that the copy constructor
cannot be a template, or that all of the parameters except the
first must have default arguments.

A note after this definition refers §12.8, but since notes
aren't normative, I don't think that this changes anything in
the situation. Also, the fact that the words "copy constructor"
are italicized in §12.1/10 indicates that this paragraph is
meant to be the official definition of the term.

I'd say that a defect report is in order, unless this has
already been fixed.

--
James Kanze GABI Software
Conseils en informatique orientée objet/
Beratung in objektorientierter Datenverarbeitung
9 place Sémard, 78210 St.-Cyr-l'École, France, +33 (0)1 30 23 00 34


[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]


Back to top
Francis Glassborow
Guest





PostPosted: Wed Dec 28, 2005 12:29 pm    Post subject: Re: What is a copy constructor, exactly? Reply with quote

In article <1135705818.322480.12610 (AT) o13g2000cwo (DOT) googlegroups.com>, kanze
<kanze (AT) gabi-soft (DOT) fr> writes
Quote:

The first also neglects to mention that the copy constructor
cannot be a template, or that all of the parameters except the
first must have default arguments.

A note after this definition refers §12.8, but since notes
aren't normative, I don't think that this changes anything in
the situation. Also, the fact that the words "copy constructor"
are italicized in §12.1/10 indicates that this paragraph is
meant to be the official definition of the term.

I'd say that a defect report is in order, unless this has
already been fixed.

It has been.

--
Francis Glassborow ACCU
Author of 'You Can Do It!' see http://www.spellen.org/youcandoit
For project ideas and contributions: http://www.spellen.org/youcandoit/projects


[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]


Back to top
Display posts from previous:   
Post new topic   Reply to topic    C++Talk.NET Forum Index -> C++ Language (Moderated) All times are GMT
Goto page 1, 2  Next
Page 1 of 2

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum


Powered by phpBB © 2001, 2006 phpBB Group
SEO toolkit © 2004-2006 webmedic.