Boost logo

Boost :

From: Borgerding, Mark A. (MarkAB_at_[hidden])
Date: 2000-04-05 10:52:20


I found another bug with MSVC that rears its ugly head with a (patched)
shared_ptr.

Beman,
The patches I sent you on 3/22 unearth some funky problems in an unrelated
part of the class. I suggest we shelve the patches for now and still
consider MSVC to be completely broken wrt template members.

I am sending this to the list to fully describe the problem.

Here is the problem:

The following code should not compile ( by the shared_ptr definition ).
   {
      boost::shared_ptr<int> spI;
      spI = new int(5); // <= this should give an error
      *spI = 12;
   }

It should not compile because:
-- There is no shared_ptr::operator=(T*)
-- The shared_ptr constructor is explicit, so the new int* cannot be
converted to a temporary.

MSVC lets the above fly without even a warning, even at level 4.

At runtime the second statement doesn't do *anything*. It's like it's not
there. This causes the third statement to puke, because spI is still NULL.

It seems to have something to do with the template member operator=. I
tried it with the baseline shared_ptr, and MSVC behaves correctly (compile
error). I have attached a file that demonstrates the crux of the problem.
I will also send a copy to MS, on the off-chance they care about the quality
of their compiler.

This raises two questions:
1) Why isn't there an shared_ptr::operator=(T*)? Or for that matter am
auto_ptr::operator=(T*)? I don't see the dangers with this that are
inherent with automatic conversions or implicitly called constructors.

2) Assuming there is a good reason for #1: What (if anything) should be
done to make this less error-prone for MSVC users. I understand that the
boost policy is not to bend over backwards to accomodate non-compliant
compilers, but a run-time error of this magnitude is pretty severe. It
could be a barrier to acceptance. Some people might blame the boost library
for the error, rather than MSVC. I think a private operator=(T*) could be
added to shared_ptr to give MSVC a clue.

-- Mark Borgerding




Boost list run by bdawes at acm.org, gregod at cs.rpi.edu, cpdaniel at pacbell.net, john at johnmaddock.co.uk