D
dragoncoder
Hi all,
I am trying to understand the auto_ptr_ref role in the implementation
of auto_ptr<>. I read the information on net but still not 100% sure of
it. My plan is as follows.
1. To see the behaviour of std::auto_ptr.
2. To implement a pkt::auto_ptr without auto_ptr_ref.
3. Check out the limitations of pkt::auto_ptr as compared to
std::auto_ptr (using steps 1 and 2).
4. Try to relate the concept of auto_ptr_ref to fill the gaps found in
step 3.
NOTE: Any siggestions on this are welcome.
As the first step I have written the following program.
=> cat example.cxx
#include <iostream>
#include <memory>
using namespace std;
template <typename T>
auto_ptr <T> source ( T a )
{
return auto_ptr <T> ( new T ( a ) );
}
template <typename T>
void print ( /* const */ auto_ptr <T>& p )
{
cout << "In print(): " << *p << endl;
}
int main()
{
print ( source ( 5 ) );
return 0;
}
z852378@premier:/home/z852378/rnd/pkt
=> g++ example.cxx
example.cxx: In function `int main()':
example.cxx:20: could not convert `source(T) [with T = int]()' to `
std::auto_ptr<int>&'
example.cxx:14: in passing argument 1 of `void print(std::auto_ptr<T>&)
[with T
= int]'
Uncommenting the 'const' in print() definition compiles the code fine
and the output is also fine. Based on my current knowledge, this is
because the return value of source() is a temporary and therefore can
not be bound to a non-const reference. Is my understanding correct ?
I am trying to understand the auto_ptr_ref role in the implementation
of auto_ptr<>. I read the information on net but still not 100% sure of
it. My plan is as follows.
1. To see the behaviour of std::auto_ptr.
2. To implement a pkt::auto_ptr without auto_ptr_ref.
3. Check out the limitations of pkt::auto_ptr as compared to
std::auto_ptr (using steps 1 and 2).
4. Try to relate the concept of auto_ptr_ref to fill the gaps found in
step 3.
NOTE: Any siggestions on this are welcome.
As the first step I have written the following program.
=> cat example.cxx
#include <iostream>
#include <memory>
using namespace std;
template <typename T>
auto_ptr <T> source ( T a )
{
return auto_ptr <T> ( new T ( a ) );
}
template <typename T>
void print ( /* const */ auto_ptr <T>& p )
{
cout << "In print(): " << *p << endl;
}
int main()
{
print ( source ( 5 ) );
return 0;
}
z852378@premier:/home/z852378/rnd/pkt
=> g++ example.cxx
example.cxx: In function `int main()':
example.cxx:20: could not convert `source(T) [with T = int]()' to `
std::auto_ptr<int>&'
example.cxx:14: in passing argument 1 of `void print(std::auto_ptr<T>&)
[with T
= int]'
Uncommenting the 'const' in print() definition compiles the code fine
and the output is also fine. Based on my current knowledge, this is
because the return value of source() is a temporary and therefore can
not be bound to a non-const reference. Is my understanding correct ?