Topic: Imm vs. on-demand gen of copy-ctr, etc


Author: teklogic_osd@apollo.is.co.za (Teklogic - Altech Systems)
Date: 1 Aug 1994 14:02:56 GMT
Raw View
There goes a question that has two answers: RTFM and real one. I am
asking for the real one, please :-)) (and, yes, I checked FAQs etc.)

The compiler shall generate copy constructors and things like this if I
do not provide such. Good. The question:
        Shall the compiler ever try to generate this code, copy-ctor
        for example, if it (the code) is never called?
Example follows:
--- cut here ---
class A {
public:
   A(int i);
private:
   A(const A &);
   int _i;
};

class B {
public:
   B(int);
private:
   A    _a;
   int  _i;
};


A::A(int i) : _i(i) {}
B::B(int i) : _a(i), _i(i) {}

int
main(void)
{
   B b(0);
   // B bc(b); -- *the* line
   return 0;
}
--- cut here ---

As the code is written -- with *the* line commented out -- some compilers
that I tried are happy some are not. As expected if you uncomment *the*
line all do complain.

Is there specified behavior for the compiler or not? If yes what's it?
If not why not?

Intuitively I tend to prefer the "always generate" option...

And one thing more -- can someone pls check what does happen on some
M*crosoft compiler, i have no one handy, and e-mail me at
the address *bellow*.

Thanx.

cheers,
shu

shu@ufrmsa1.olivetti.za -- please respond to this address which is not the
address where your message will go if you press `r' now. thanx.




Author: fjh@munta.cs.mu.OZ.AU (Fergus Henderson)
Date: Thu, 4 Aug 1994 16:51:10 GMT
Raw View
teklogic_osd@apollo.is.co.za (Teklogic - Altech Systems) writes:

>The compiler shall generate copy constructors and things like this if I
>do not provide such. Good. The question:
>        Shall the compiler ever try to generate this code, copy-ctor
>        for example, if it (the code) is never called?

No.  The definitions for default constructors, copy constructors, etc.,
are only generated if they are called.

Reference: ANSI/ISO Working Paper, 12.1 [class.ctor], paragraphs 4 & 5.

--
Fergus Henderson - fjh@munta.cs.mu.oz.au