dasjotre said:
(I don't use g++).
in the code:
boost::shared_ptr<int> pi = new int; // 1
pi = new int; // 2
line 1 should not give you any warning at
all since it is equivalent to
boost::shared_ptr<int> pi (new int);
line 2 should give you a compile error.
shared.cpp:1: error: conversion from 'int*' to non-scalar type
'boost::shared_ptr<int>' requested
shared.cpp:2: error: no match for 'operator=' in 'pi = (int*)operator new(4u)'
line 1 is not equivalent to boost::shared_ptr<int> pi (new int);
also,
boost::shared_ptr<int> pi = boost::shared_ptr<int>(new int);
is not equivalent to
boost::shared_ptr<int> pi (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:
boost::shared_ptr<int> pi = boost::shared_ptr<int>(new int);
pi = boost::shared_ptr<int>(new int);
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.