Re: auto_ptr conversion derived to base
perplexing.4.peterdulimov@spamgourmet.com wrote:
Hello,
I get differing results for the following code on differing platforms.
Could someone please advise if what I have written is legal? I found a
reference to an older document that seems to intimate that this might
be ok. (Look right at the end)...
http://www.awprofessional.com/content/images/020163371X/autoptrupdate/auto_p
tr_update.html
TIA,
Peter Dulimov.
[email address is my first name followed by the first two letters of my
surname at
resmedA dot comB dot auC, without capitals.]
------------8<-------------------8<-------------------8<-----------------
#include <memory>
#include <iostream>
#include <ostream>
#include <fstream>
#include <iomanip>
class Test
{
std::auto_ptr<std::ostream> m_pOs;
public:
Test(std::auto_ptr<std::ostream> o) : m_pOs(o)
{
*m_pOs << "Hello" << std::endl;
}
private:
Test(const Test& other);
const Test& operator=(const Test& rhs);
};
int main(int argc, char* argv[])
{
Test question(std::auto_ptr<std::ofstream>(new
std::ofstream("file")));
return 0;
}
------------8<-------------------8<-------------------8<-----------------
This is a known peculiarity of std::auto_ptr. Derived to base auto_ptr
conversion can only be performed when using direct initialization, but
not copy initialization. As the current language does not provide
r-value references it is simulated in part by auto_ptr and auto_ptr_ref
obscure interaction which is the cause of the inability to convert when
using copy initialization
Function arguments are initialized using copy initialization, this is
why you can not pass a derived auto_ptr to a function expecting a base
one. Consider this code:
#include <memory>
struct A {};
struct B : A {};
int main(int argc, char* argv[])
{
// direct initialization
std::auto_ptr<A> a0(std::auto_ptr<A>(0)); // okay
std::auto_ptr<A> b0(std::auto_ptr<B>(0)); // okay
// copy initialization
std::auto_ptr<A> a1 = std::auto_ptr<A>(0); // okay
std::auto_ptr<A> b1 = std::auto_ptr<B>(0); // error
}
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]