c++ - auto_ptr_ref implementation questions -


i have looked @ different sources regard internal implementation of auto_ptr<> , auto_ptr_ref<>. have question couldn't figure out why.

.... when returning 'auto_ptr' function, compiler finds there no suitable ctor copy construct returned object. there conversion 'auto_ptr_ref' , ctor taking 'auto_ptr_ref' constructing 'auto_ptr'. thus, compiler creates 'auto_ptr_ref' holds reference original 'auto_ptr' , constructs 'auto_ptr' object. that's (well, when returning object, the compiler goes through process twice because returned value copied somewhere not change process).... (reference http://www.josuttis.com/libbook/auto_ptr.html)

in example, emulate implementation of auto_ptr<> , auto_ptr_ref<> , able generate results showing compiler indeed goes through process twice

the example has these basic traits:

1) a's copy constructor not take const reference. 2) has conversion operator b 3) has constructor a(b);

class b { };  class { public:  () {     printf("a default constructor() @ %p\r\n", this); }  a(b) {     printf("constructor(b) @ %p\r\n", this); } (a &a) {     printf("copy constructor(non-const) @ %p\r\n", this); }  operator b() {     printf("a convertion constructor(b) @ %p\r\n", this);     return b(); } };   foo() {     return a(); }  int main() {     a(foo()); } 

so when a a(foo())) executed

1) foo() generates temporary object x 2) x converted type b 3) constructor a(b) used construct object a

this output:

a default constructor() @ 0xbfea340f convertion constructor(b) @ 0xbfea340f constructor(b) @ 0xbfea344f convertion constructor(b) @ 0xbfea344f constructor(b) @ 0xbfea344e 

we can see compiler wen through conversion-construct steps 2&3 twice.

why that?

if calling function returns class type, following happens:

  • on call, calling function reserves space temporary return value on stack (it has done calling function because called function allocates on stack gets deallocated function returns). passes address of space called function.

  • on executing return statement, called function constructs return value argument given return statement space provided calling function. in case, argument temporary a value.

  • after function returns, caller uses temporary value called function constructed whatever code demands. in case, use construct local variable it.

so construct new object of type a existing 1 twice: first, construct temporary return value (whose life time persists after function foo returns, until end of declaration) explicitly generated temporary in return statement (whole lifetime ends on end of full expression, in case equivalent on return foo). , second, initialize local variable a temporary returned foo.

of course, due no appropriate copy constructor being available, in both cases go through b conversions. works because return direct initialization, , a explicitly coded such direct initialization.


Comments

Popular posts from this blog

javascript - DIV "hiding" when changing dropdown value -

Does Firefox offer AppleScript support to get URL of windows? -

android - How to install packaged app on Firefox for mobile? -