 |
C++Talk.NET C++ language newsgroups
|
| View previous topic :: View next topic |
| Author |
Message |
Rodrigo Strauss Guest
|
Posted: Sat Apr 09, 2005 10:52 pm Post subject: Why copy constructors are automatically generated? |
|
|
Sometimes, generated copy constructor just make you code buggy (when
you have member raw pointers, for example). Sometimes you don't really
want a copy constructor. I know I can create a private one, but why is
it generated? Maybe it would be better to require programmer to specify
the need for the generated one (like declaring but not implementing).
Most of times C++ compilers don't generate code and this is why it's so
fast and so flexible. Why copy constructors don't follow this
philosophy? C-language compatibility?
I've found no mention to this in the Stroustrup's FAQ. Is it so
obvious?
Rodrigo Strauss
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
|
|
| Back to top |
|
 |
Mark Van Peteghem Guest
|
Posted: Sun Apr 10, 2005 2:00 pm Post subject: Re: Why copy constructors are automatically generated? |
|
|
Rodrigo Strauss wrote:
| Quote: | Sometimes, generated copy constructor just make you code buggy (when
you have member raw pointers, for example). Sometimes you don't really
want a copy constructor. I know I can create a private one, but why is
it generated? Maybe it would be better to require programmer to specify
the need for the generated one (like declaring but not implementing).
Most of times C++ compilers don't generate code and this is why it's so
fast and so flexible. Why copy constructors don't follow this
philosophy? C-language compatibility?
I presume it is because of C compatibility, which is a very good |
reason. I think it is also the fastest solution, because the
generated code will probably do a simple memcpy, and there isn't
anything faster than that. But I agree that it can make your code
buggy.
--
Mark dot Van dot Peteghem at q-mentum dot com
http://www.q-mentum.com -- easier and more powerful unit testing
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
|
|
| Back to top |
|
 |
Kim, Seungtai Guest
|
Posted: Sun Apr 10, 2005 7:16 pm Post subject: Re: Why copy constructors are automatically generated? |
|
|
"Mark Van Peteghem"
| Quote: | ...because the
generated code will probably do a simple memcpy, and there isn't
anything faster than that. ...
|
The generated copy-constructor dose not use the memcpy for copying
object. It dose memberwise copy for all the subobjects by the other
copy-ctors and (user-defined) assignment operators.
--
S Kim <stkim (AT) yujinrobot (DOT) com>
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
|
|
| Back to top |
|
 |
Antoun Kanawati Guest
|
Posted: Sun Apr 10, 2005 7:16 pm Post subject: Re: Why copy constructors are automatically generated? |
|
|
Mark Van Peteghem wrote:
| Quote: | Sometimes, generated copy constructor just make you code buggy (when
you have member raw pointers, for example). Sometimes you don't really
want a copy constructor. I know I can create a private one, but why is
it generated? Maybe it would be better to require programmer to specify
the need for the generated one (like declaring but not implementing).
Most of times C++ compilers don't generate code and this is why it's so
fast and so flexible. Why copy constructors don't follow this
philosophy? C-language compatibility?
I presume it is because of C compatibility, which is a very good
reason. I think it is also the fastest solution, because the
generated code will probably do a simple memcpy, and there isn't
anything faster than that. But I agree that it can make your code
buggy.
|
The memcpy semnatics have not been used for a very long time. C++
uses member-wise copy/assignment semantics.
Also, if I recall correctly, the copy/assign generation had to do more
with making user defined types behave like builtin types, which can
be copied and assigned. That would be the "philosophical" aspect.
As for bugginess: C++ has always generated copy-ctor and op= if the
programmer did not define them; so, as long as you keep that in the
foreground, instead of the background, it's hard to forget about it
and get into the buggy situation. The extra bugginess is usually
because the need to manage copy/assign is left as an afterthought, or
"discovered" while debugging a [detected] runtime problem.
--
A. Kanawati
[email]NO.antounk.SPAM (AT) comcast (DOT) net[/email]
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
|
|
| Back to top |
|
 |
Ron Natalie Guest
|
Posted: Sun Apr 10, 2005 10:39 pm Post subject: Re: Why copy constructors are automatically generated? |
|
|
Antoun Kanawati wrote:
| Quote: |
The memcpy semnatics have not been used for a very long time. C++
uses member-wise copy/assignment semantics.
Of course, if the members have simple copy semantics (i.e., the whole |
thing is POD), the compiler will tend to use a block move that may in
fact be faster than the generic memcpy as assumptions can be made about
the size and the alignment.
[ 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
|
Posted: Mon Apr 11, 2005 7:02 am Post subject: Re: Why copy constructors are automatically generated? |
|
|
Rodrigo Strauss wrote:
| Quote: | Sometimes, generated copy constructor just make you code buggy (when
you have member raw pointers, for example). Sometimes you don't really
want a copy constructor. I know I can create a private one, but why is
it generated? Maybe it would be better to require programmer to
specify the need for the generated one (like declaring but not
implementing).
Most of times C++ compilers don't generate code and this is why it's
so fast and so flexible. Why copy constructors don't follow this
philosophy? C-language compatibility?
I've found no mention to this in the Stroustrup's FAQ. Is it so
obvious?
|
Most obvious reason is C compatibility. C supports assignment of
structs by default, therefore C++ must for (at a minimum) support
it for POD types. In practice, supporting it for POD types but
not for other types, would be an anomoly in the language for
compiler writers to deal with and to frustrate programmers.
The default generated copy constructor for all types is "member-wise
copy" semantics. The only cause of inefficiency in that is related
to the fact that this means calling user-defined constructors (if
any).
AS to it causing bugs: the basic rule of thumb is that anything
which involves sharing a raw pointer between two objects can
introduce bugs. If you're doing such things, the process of
designing the class should identify that it has raw pointers,
so there needs to be some thought as to whether it requires
a copy constructor to be explicitly written, of if copying
should be disabled (eg make the copy constructor private).
And, if you do it to a copy constructor, also do it with the
assignment operator.....
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
|
|
| Back to top |
|
 |
Dmitry Mityugov Guest
|
Posted: Mon Apr 11, 2005 7:34 am Post subject: Re: Why copy constructors are automatically generated? |
|
|
Rodrigo Strauss wrote:
| Quote: | Sometimes, generated copy constructor just make you code buggy (when
you have member raw pointers, for example). Sometimes you don't really
want a copy constructor. I know I can create a private one...
|
You can also declare (but not define) a copy constructor (not necessarily
private). This will prevent the compiler from generating it, and a linker
error will tell you if there are attempts in your code to call it. BTW, if
you don't want a copy constructor, you probably do not want an assignment
operator either. This is an example:
class CODate {
public:
...
// declared but not defined:
CODate( CODate const& date);
CODate& operator=( CODate const& date);
Dmitry
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
|
|
| Back to top |
|
 |
R.F. Pels Guest
|
Posted: Mon Apr 11, 2005 1:36 pm Post subject: Re: Why copy constructors are automatically generated? |
|
|
Rodrigo Strauss wrote:
| Quote: | Sometimes, generated copy constructor just make you code buggy (when
you have member raw pointers, for example). Sometimes you don't really
want a copy constructor. I know I can create a private one, but why is
it generated?
|
It is to preserve the same copy and assignment semantics as primitive types.
You would be rudely surprised if objects of a class you define yourself
cannot do the same thing as let's say an int.
Looking at it from the opposite side: primitive types like int, float
etcetera also have a copy constructor and an assignment operator. It is as
if a copy constructor and an assignment operator is generated for them too.
You can for example do:
int x(5);
int y(x);
which looks very much the same as using a copy constructor to initialize an
object, or:
int p = 10;
int q = p;
which amounts to using int::operator(int& that) to initialize the value.
Now, in order to prevent surprises, James Coplien tells us that a programmer
should implement a default constructor, a copy constructor, an assignment
operator and a destructor for any nontrivial class. He calls this 'Orthodox
Canonical Form'.
--
Ruurd
..o.
...o
ooo
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
|
|
| Back to top |
|
 |
Ivan Krivyakov Guest
|
Posted: Tue Apr 12, 2005 6:55 am Post subject: Re: Why copy constructors are automatically generated? |
|
|
"R.F. Pels" <spamtrap (AT) tiscali (DOT) nl> wrote
| Quote: |
You would be rudely surprised if objects of a class you define yourself
cannot do the same thing as let's say an int.
|
No, I would not. After all, my class is not an int.
Or, if it is, why stop at copying and assignment?
Where is my automatically generated operator+, operator* and other friends?
And finally, where is that lovely weird 0 constant that always gets mixed
with NULL pointer during overloads? :-)
| Quote: | Looking at it from the opposite side: primitive types like int, float
etcetera also have a copy constructor and an assignment operator.
|
They have many other things as well.
I think, root of the problem is C structs, not primitive types.
| Quote: |
Now, in order to prevent surprises, James Coplien tells us that a
programmer
should implement a default constructor, a copy constructor, an assignment
operator and a destructor for any nontrivial class. He calls this
'Orthodox
Canonical Form'.
|
Well, from modern "defensive" programming point of view this considered
a bad thing.
In Java/C# you must know what you're doing, but if you try to do something
really stupid (at the low level), it is either impossible, or compiler will
warn you.
In C you must know what you're doing, and if you do something stupid,
so be it. No stop signs, speed limit, nobody's gonna slow you down
However, if you don't explicitly do something stupid, compiler will never
silently do it for you. If you did not write it, it's not there. Zero
initialization
of statics does not count.
In C++ you must know not only what you're doing, but also what
compiler is doing on your behalf. If you do something stupid, or
if compiler does something stupid and you overlook it,
it's your fault.
I am afraid C++ got the worst combination of them all.
Ivan
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
|
|
| Back to top |
|
 |
Dmitry Mityugov Guest
|
Posted: Tue Apr 12, 2005 8:58 pm Post subject: Re: Why copy constructors are automatically generated? |
|
|
R.F. Pels wrote:
....
| Quote: | Now, in order to prevent surprises, James Coplien tells us that a
programmer
should implement a default constructor, a copy constructor, an assignment
operator and a destructor for any nontrivial class. He calls this
'Orthodox
Canonical Form'.
|
Why is there a default constructor in this list? If another constructor,
like a copy constructor, is declared for a class, the compiler will not
automatically generate a default constructor, will it?
Dmitry
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
|
|
| Back to top |
|
 |
Antoun Kanawati Guest
|
Posted: Wed Apr 13, 2005 6:46 am Post subject: Re: Why copy constructors are automatically generated? |
|
|
Dmitry Mityugov wrote:
| Quote: | R.F. Pels wrote:
...
Now, in order to prevent surprises, James Coplien tells us that a
programmer
should implement a default constructor, a copy constructor, an assignment
operator and a destructor for any nontrivial class. He calls this
'Orthodox
Canonical Form'.
Why is there a default constructor in this list? If another constructor,
like a copy constructor, is declared for a class, the compiler will not
automatically generate a default constructor, will it?
|
Correct. So, if you don't have a default ctor, you will get surprised,
unpleasantly, when you try to use the standard containers, some of which
impose default-ctor requirements on the element type.
My version of this rules is that I need to address the enumerated
methods: as in, if there is no default-ctor, it is not because I
forgot about it [usually :-]
The copy/assign/destroy are usually coupled: assign and copy are
essentially variations on a theme, and if you need to hand-code
either of them, you need to do the other as well. As for the
destructor: any time you need to hand-code copy/assign, including
inhibiting them, there is a good chance that you will need to write
the destructor yourself. Conversely, if you have a hand-coded
destructor, it is almost always the case that you will also need to
pay extra attention to copy/assign operations.
--
A. Kanawati
[email]NO.antounk.SPAM (AT) comcast (DOT) net[/email]
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
|
|
| Back to top |
|
 |
kerzum@mail.ru Guest
|
Posted: Wed Apr 13, 2005 6:50 am Post subject: Re: Why copy constructors are automatically generated? |
|
|
| Quote: | No, I would not. After all, my class is not an int.
Or, if it is, why stop at copying and assignment?
Where is my automatically generated operator+, operator* and other
friends?
|
Well, you're kidding - but what's really missing is
automatically-generated
operator<. I'm serious IMO it should recursively invoke
operator< of all members in the order of their declaration to produce a
stable lexicographical sequence. Writing this manually is boring - and
this is
even impossible to use any metaprogramming tricks here.
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
|
|
| Back to top |
|
 |
Bo Persson Guest
|
Posted: Wed Apr 13, 2005 8:01 pm Post subject: Re: Why copy constructors are automatically generated? |
|
|
<kerzum (AT) mail (DOT) ru> skrev i meddelandet
news:1113350670.039284.296770 (AT) z14g2000cwz (DOT) googlegroups.com...
| Quote: | No, I would not. After all, my class is not an int.
Or, if it is, why stop at copying and assignment?
Where is my automatically generated operator+, operator* and other
friends?
:-)
Well, you're kidding - but what's really missing is
automatically-generated
operator<. I'm serious IMO it should recursively invoke
operator< of all members in the order of their declaration to produce
a
stable lexicographical sequence. Writing this manually is boring - and
this is
even impossible to use any metaprogramming tricks here.
|
But this breaks immediately for complex numbers, where you don't have an
ordering.
How will the compiler know that name-and-address records should be
sorted on the zip code while printing, and by last name while searching?
It just doesn't make any sense.
Bo Persson
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
|
|
| Back to top |
|
 |
Ron Natalie Guest
|
Posted: Wed Apr 13, 2005 8:06 pm Post subject: Re: Why copy constructors are automatically generated? |
|
|
Antoun Kanawati wrote:
| Quote: | Correct. So, if you don't have a default ctor, you will get surprised,
unpleasantly, when you try to use the standard containers, some of which
impose default-ctor requirements on the element type.
No standard container requires a default constructor. The functions |
that need to use "fill" objects take an additional argument (defaulted
to a default constructed object) to copy to the "empty" position.
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
|
|
| Back to top |
|
 |
Antoun Kanawati Guest
|
Posted: Thu Apr 14, 2005 7:16 am Post subject: Re: Why copy constructors are automatically generated? |
|
|
Ron Natalie wrote:
| Quote: | Antoun Kanawati wrote:
Correct. So, if you don't have a default ctor, you will get surprised,
unpleasantly, when you try to use the standard containers, some of which
impose default-ctor requirements on the element type.
No standard container requires a default constructor. The functions
that need to use "fill" objects take an additional argument (defaulted
to a default constructed object) to copy to the "empty" position.
|
It's only one implementation, but that's all I have to work with.
And, it wants a default ctor.
$ cat m.cpp
#include <map>
struct Foo {
Foo(int) { }
};
int main()
{
std::map<int, Foo> m;
m[3] = Foo(42);
return 0;
}
5$ g++ -c m.cpp
...../bits/stl_map.h: In member function `_Tp& std::map<_Key, _Tp,
_Compare, _Alloc>::operator[](const _Key&) [with _Key = int, _Tp = Foo,
_Compare = std::less<int>, _Alloc = std::allocator<std::pair
Foo> >]':
m.cpp:10: instantiated from here
...../bits/stl_map.h:339: error: no matching function for call to `Foo::Foo()'
m.cpp:3: note: candidates are: Foo::Foo(const Foo&)
m.cpp:4: note: Foo::Foo(int)
--
A. Kanawati
[email]NO.antounk.SPAM (AT) comcast (DOT) net[/email]
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
|
|
| Back to top |
|
 |
|
|
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
|
|