 |
C++Talk.NET C++ language newsgroups
|
| View previous topic :: View next topic |
| Author |
Message |
Pramod Guest
|
Posted: Wed Jul 28, 2004 1:58 am Post subject: When are default functions not generated |
|
|
Hi,
Under what circumstances are the default functions are not generated
by the compiler?
(oncstructor,destructor,copy constructor, assignment operator, address
operators).
I know one case in which assignment operator won't be constructed and
that is when we have reference or constant memebers.
What are ALL such cases when compiler refuses to generate these
functions.
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
|
|
| Back to top |
|
 |
Victor Bazarov Guest
|
Posted: Wed Jul 28, 2004 3:15 am Post subject: Re: When are default functions not generated |
|
|
"Pramod" <pramod_iitg (AT) yahoo (DOT) com> wrote...
| Quote: | Under what circumstances are the default functions are not generated
by the compiler?
(oncstructor,destructor,copy constructor, assignment operator, address
operators).
I know one case in which assignment operator won't be constructed and
that is when we have reference or constant memebers.
What are ALL such cases when compiler refuses to generate these
functions.
|
The compiler won't generate them if they are at least _declared_ in the
class. In that case the program is well-formed unless you try to use
(or cause the use of) any of them. In that case they have to be defined.
In any other case, AFAICT, the implicitly declared special functions
are always implicitly defined unless it's impossible to do, and in that
case the program is ill-formed.
V
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
|
|
| Back to top |
|
 |
Richard Corden Guest
|
Posted: Wed Jul 28, 2004 12:35 pm Post subject: Re: When are default functions not generated |
|
|
[email]pramod_iitg (AT) yahoo (DOT) com[/email] (Pramod) writes:
| Quote: | Hi,
Under what circumstances are the default functions are not generated
by the compiler?
(oncstructor,destructor,copy constructor, assignment operator, address
operators).
|
Firstly, there are only 4 functions that the a compiler will actually
implicitly declare or define.
default constructor
destructor
copy constructor
assignment operator.
I have heard it asked in interviews what functions will be implicitly
generated. Once a colleague of mine, who also worked on our front
end, answered 4 only to be told he was wrong!
The compiler does not implicitly generate an address of operator for
your class. The builtin address of operator does not have
restrictions on its operand type, i.e. operator+ will has restrictions
on its operands such as one should be arithmetic etc, however
operator& simply returns the address of its operand T.
Anyway, back to the question:
default constructor
-> if no constructor exists (including a copy ctor)
desctructor
-> if the destructor doesn't exist
copy constructor
-> if no copy constructor exists.
copy assignment
-> if no copy assign exists.
| Quote: | I know one case in which assignment operator won't be constructed and
that is when we have reference or constant memebers.
|
12.8/12 says the program is illformed if the class contains a member
of one of the types you mention and the implicit assignement operator
is defined. The assignement operator is implicitly declared anyway,
its just an illformed program if you try to use it.
Regards,
Richard
--
Richard Corden
To reply remove 's' from address
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
|
|
| Back to top |
|
 |
Serge N Guest
|
Posted: Thu Jul 29, 2004 3:22 pm Post subject: Re: When are default functions not generated |
|
|
"Pramod" <pramod_iitg (AT) yahoo (DOT) com> wrote
| Quote: | Hi,
Under what circumstances are the default functions are not generated
by the compiler?
(oncstructor,destructor,copy constructor, assignment operator, address
operators).
I know one case in which assignment operator won't be constructed and
that is when we have reference or constant memebers.
What are ALL such cases when compiler refuses to generate these
functions.
|
Speaking about default ctors, there are some obvious cases, e.g.
You declare/define some ctor(s) requiring arguments, but not the default
one - compiler will not
generate the defualt ctor for you.
Some not so obvious stuff:
consider such class --
class Account
{ public:
char *_name;
unsigned int _acct_nmbr;
double _balance;
};
.........
Account local_acct;
Account *heap_acct = new Account;
Now, for that class, IN PRACTICE(this is important -- in real-life,
industrial-strength compilers),
no default ctor is generated nor is one invoked.
Of course, one can argue that this information may be considered just an
implementation detail and is
not terribly useful for a developer - important thing to rememeber is that a
programmer(not compiler)
is always responsible for initializing class members of 'primitive' types.
For more details see "C++ Primer", pp.698-699
- Sergey
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
|
|
| Back to top |
|
 |
Richard Corden Guest
|
Posted: Thu Jul 29, 2004 9:18 pm Post subject: Re: When are default functions not generated |
|
|
"Serge N" <serge (AT) columbus (DOT) rr.com> writes:
[...snip...]
| Quote: | Some not so obvious stuff:
consider such class --
class Account
{ public:
char *_name;
unsigned int _acct_nmbr;
double _balance;
};
........
Account local_acct;
Account *heap_acct = new Account;
Now, for that class, IN PRACTICE(this is important -- in real-life,
industrial-strength compilers),
no default ctor is generated nor is one invoked.
|
I have a feeling that this statement is not quite correct, and there
is actually a default constructor generated. However, the *bad*
effects you describe are the same. The implicitly generated
constructor will have an empty member initialisation list.
<code>
class Account
{
public:
Account ()
{
}
};
</code>
In such a constructor, POD members are not intialised. But it doesn't
have to be an aggregate for this to be an issue:
<code>
class NonAggregateAccount
{
public:
virtual ~NonAggregateAccount () {}
private:
int i; // not initalised
};
</code>
The aggregate class can initialise its members when defining an object
of the class type. But in the above, 'i' won't be initialised at all!
[...snip...]
| Quote: | important thing to rememeber is that a programmer(not compiler) is
always responsible for initializing class members of 'primitive'
types.
|
Agreed - primitive also including 'POD'.
Regards,
Richard
--
Richard Corden
To reply remove 's' from address
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
|
|
| Back to top |
|
 |
Serge Novgorodsky Guest
|
Posted: Fri Jul 30, 2004 9:47 am Post subject: Re: When are default functions not generated |
|
|
"Richard Corden" <richards_corden (AT) hotmail (DOT) com> wrote
| Quote: | "Serge N" <serge (AT) columbus (DOT) rr.com> writes:
[...snip...]
Some not so obvious stuff:
consider such class --
class Account
{ public:
char *_name;
unsigned int _acct_nmbr;
double _balance;
};
........
Account local_acct;
Account *heap_acct = new Account;
Now, for that class, IN PRACTICE(this is important -- in real-life,
industrial-strength compilers),
no default ctor is generated nor is one invoked.
I have a feeling that this statement is not quite correct, and there
is actually a default constructor generated.
|
Not according to Stan Lippman. Plz see "C++ Primer" pp.698-699.
He provides more details on this in his "Inside the C++ Object Model".
[...snip...]
| Quote: | The implicitly generated
constructor will have an empty member initialisation list.
|
Exactly! Empty member initialisation list and empty body.
So, what set of initializations are carried out by such ctor? --
a. the invocation of all base class sub-objects with an associated
default constructor
b. the invocation of all member class objects with an associated default
constructor
In our class, above, neither case (a) nor case (b) hold. there are no
initializations that would be performed by the empty default ctor.
<quote>
"because there is nothing for the implicit ctor to do, it is not in
practice generated by any compiler on this earth -- except possibly student
compilers. the standard somewhere permits this -- that is, the elimination
of a constructor call if the absence of that call does not change the
behavior of the program.
thus, no default constructor is (1) created, and (2) invoked. "
<qoute>
The last paragraph is a quote from Stan Lippman's answer to my e-mail
regarding the same example.
| Quote: |
code
class Account
{
public:
Account ()
{
}
};
/code
In such a constructor, POD members are not intialised. But it doesn't
have to be an aggregate for this to be an issue:
code
class NonAggregateAccount
{
public:
virtual ~NonAggregateAccount () {}
private:
int i; // not initalised
};
/code
BTW, in your example the default ctor WILL be generated, |
because your class has a virtual function -- another quote from
Stans email:
<quote>
"it is true that a default constructor will be synthesized if the class
contains a
virtual function, because the implementation bookkeeping requires that the
internal pointer to a virtual function be initialized to 0 -- generalized,
that says, if the compiler needs to synthesize a default constructor, it
does so to fullfill its obligations and does not attend to your primitive
data members -- you use those at your own risk!" <qoute>
Anyway, as I said I consider this information(default ctor not generated
in practice in some cases) to be of a little practical value - it doesn't
change
or add anything new to the fact that initializing
primitive - I mean POD - class members
is ALWAYS a programmer's responsibility
Regards,
Sergey
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
|
|
| Back to top |
|
 |
Richard Corden Guest
|
Posted: Fri Jul 30, 2004 3:09 pm Post subject: Re: When are default functions not generated |
|
|
"Serge Novgorodsky" <snodgoro (AT) columbus (DOT) rr.com> writes:
[...snip...]
| Quote: | BTW, in your example the default ctor WILL be generated,
because your class has a virtual function -- another quote from
Stans email:
quote
"it is true that a default constructor will be synthesized if the class
contains a
virtual function, because the implementation bookkeeping requires that the
internal pointer to a virtual function be initialized to 0 -- generalized,
that says, if the compiler needs to synthesize a default constructor, it
does so to fullfill its obligations and does not attend to your primitive
data members -- you use those at your own risk!"
Anyway, as I said I consider this information(default ctor not generated
in practice in some cases) to be of a little practical value - it doesn't
change
or add anything new to the fact that initializing
primitive - I mean POD - class members
is ALWAYS a programmer's responsibility
|
I believe I misread your original post. The example of the aggregate
class made me think you were saying: "because there is no implicitly
generated default ctor, the members are not initialised".
So it seems we've been arguing on the same side!
Regards,
Richard
--
Richard Corden
To reply remove 's' from address
[ 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
|
Posted: Fri Jul 30, 2004 3:25 pm Post subject: Re: When are default functions not generated |
|
|
In article <pljOc.4722$Bc6.253 (AT) fe1 (DOT) columbus.rr.com>, Serge Novgorodsky
<snodgoro (AT) columbus (DOT) rr.com> writes
| Quote: | Anyway, as I said I consider this information(default ctor not generated
in practice in some cases) to be of a little practical value - it doesn't
change
or add anything new to the fact that initializing
primitive - I mean POD - class members
is ALWAYS a programmer's responsibility
|
Exactly, so arguing about whether a do-nothing function is generated or
not is pretty pointless. We are not allowed to take the address of a
ctor so how do you determine if an implicitly generated ctor exists or
not from within a program where the as-if rule reigns supreme.
It is very hard to see how we should implement an implicitly generated
inline do-nothing ctor. Perhaps we should include a no-op (to match the
padding byte of a stateless class);-)
However, note that in the case of a static instance of a POD some form
of default initialisation is required and the programmer is not required
to take any explicit action. There are also other cases such as:
struct x {
int i;
double d;
};
int main(){
x array[100] = {12};
}
--
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 |
|
 |
|
|
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
|
|