N
Neelesh Bodas
Hello All,
My question is regarding the implementation of auto pointers - How
does the class auto_ptr_ref work?
eg: suppose I have :
auto_ptr<int> foo();
int bar()
{
auto_ptr<int*> k = foo();
}
The exact sequence of calls as I see here is (no optimizations assumed)
-
1) foo returns auto_ptr<int> by value
2) CC is called on the return value.
3) The returned auto_ptr<int> is converted to auto_ptr_ref<int> by
using operator auto_ptr_ref
4) Now since this is a copy initialization and not a direct
initialization, the RHS (whose type is now auto_ptr_ref<int>) needs to
get converted to auto_ptr<int> again - done by ctor of auto_ptr<int>
which takes auto_ptr_ref<int>
5) Now CC should be called - but again we are back to old problem - the
generated auto_ptr is temporary and hence cannot be passed as a
non-const reference to the function.
Seems I am going somewhere in cycle :-(
Josuttis' book doesnot say enough on this -
<quote>
"The mechanism relies on a slight difference between the overloading
and template argument deduction rules. This difference is too subtle to
be of use as a general programming tool, but it is sufficient to enable
the auto_ptr class to work correctly."
</quote>
Can somebody explain what I am not getting here?
My question is regarding the implementation of auto pointers - How
does the class auto_ptr_ref work?
eg: suppose I have :
auto_ptr<int> foo();
int bar()
{
auto_ptr<int*> k = foo();
}
The exact sequence of calls as I see here is (no optimizations assumed)
-
1) foo returns auto_ptr<int> by value
2) CC is called on the return value.
3) The returned auto_ptr<int> is converted to auto_ptr_ref<int> by
using operator auto_ptr_ref
4) Now since this is a copy initialization and not a direct
initialization, the RHS (whose type is now auto_ptr_ref<int>) needs to
get converted to auto_ptr<int> again - done by ctor of auto_ptr<int>
which takes auto_ptr_ref<int>
5) Now CC should be called - but again we are back to old problem - the
generated auto_ptr is temporary and hence cannot be passed as a
non-const reference to the function.
Seems I am going somewhere in cycle :-(
Josuttis' book doesnot say enough on this -
<quote>
"The mechanism relies on a slight difference between the overloading
and template argument deduction rules. This difference is too subtle to
be of use as a general programming tool, but it is sufficient to enable
the auto_ptr class to work correctly."
</quote>
Can somebody explain what I am not getting here?