Why is the following code not valid? I mean, I see the code and it
doesn't allow it, but I am curious about the rationale?

boost::shared_ptr<intpi = new int;
pi = new int;

(you are aware that there is no operator=(raw ptr) in shared_ptr)

IMHO the clue is in the explicit constructor of shared_ptr

so something like

void foo(shared_ptr<intconst & p)

can not be used with

int * p = new int;
void foo(p);

delete p; // UB !

Ahh, of course. I get that, then.

boost::shared_ptr<intpi = new int;

can not unwind into

boost::shared_ptr<intpi = boost::shared_ptr<int>(new int);

This is supposed to call the constructor, isn't it? Why can it not
figure out (or not be allowed) to call the (int *) ctor?

Thanks

Tim

(sorry for my clumsy reply)

boost::shared_ptr<intpi = new int;
actually calls the constructor. the consequent
pi = new int;
can not. since pi is already constructed object
the assignment operator is looked up and there
are two assignment operators in shared_ptr
one that takes shared_ptr and one that takes
std::auto_ptr. neither of these two are implicitly
constructible from raw pointer, so neither of them
can be used.

that is, compiler can not convert
pi = new int;
into either
pi = shared_ptr<int>(new int);
or
pi = auto_ptr<int>(new int);
DS

line 1 is not equivalent to boost::shared_ptr<intpi (new int);
also,
boost::shared_ptr<intpi = boost::shared_ptr<int>(new int);
is not equivalent to
boost::shared_ptr<intpi (new int);
since you need a copy constructor for the first (the compiler complains if
you haven't, but it may optimize it away).

So in both lines you need to _explicitly_ construct a temporary shared_ptr:

This was made so that the compiler does not accidentaly construct a
shared_ptr from a raw pointer and free it afterwards. This could cause bugs
like double frees or calling delete on automatic (stack)-objects.

line 1 is not equivalent to boost::shared_ptr<intpi (new int);
also,
boost::shared_ptr<intpi = boost::shared_ptr<int>(new int);
is not equivalent to
boost::shared_ptr<intpi (new int);
since you need a copy constructor for the first (the compiler complains if
you haven't, but it may optimize it away).

So in both lines you need to _explicitly_ construct a temporary shared_ptr:

This was made so that the compiler does not accidentaly construct a
shared_ptr from a raw pointer and free it afterwards. This could cause bugs
like double frees or calling delete on automatic (stack)-objects.

I stand corrected.

1. will call
boost::shared_ptr<intpi = new int;
will try to call boost::shared_ptr<int>::operator=(raw ptr)
and will fail for the same reason as 2.
pi = new int;

most compilers will call constructor on 1 (not standard
behaviour), and I got lost thinking about the rationale
for explicit shared_ptr/auto_ptr constructors.