Boost logo

Boost :

From: David Abrahams (abrahams_at_[hidden])
Date: 2000-10-01 15:14:30


Your approach invokes undefined behavior, which is one reason the compiler
complains when you use the safe C++-style casts. Even if that weren't so, it
isn't going to work since it is the specific shared_ptr<T> type which
handles the destruction of T. Your best bet is to take bjarne's advice,
then augment your container with a wrapper that calls delete on each element
and accepts an auto_ptr<> for insertion.

Good luck,

----- Original Message -----
From: "Lars Thorup" <lars.thorup_at_[hidden]>
To: "Boost" <boost_at_[hidden]>
Sent: Sunday, October 01, 2000 3:01 PM
Subject: [boost] shared_ptr<void> produces warning

> Hi,
> I have problems compiling the following small piece of code on
> egcs-2.91.66 with the -Wall option:
> shared_ptr<void> spv(new int(4));
> shared_ptr<int> spi(static_cast< shared_ptr<int> >(spv));
> The compiler gives the following warning:
> /usr/local/include/boost/smart_ptr.hpp:138: warning: ANSI C++ forbids
> implicit conversion from `void *' in assignment
> However the "equivalent" c-style code gives no warnings:
> void* pv = new int(4);
> int* pi(static_cast<int*>(pv));
> The reason I want to do this, is because I want to specialize the STL
> containers on shared_ptr<T> using a container of shared_ptr<void> as the
> common implementation. This idea is recommended by Bjarne Stroustrup in
> "The C++ Programming Language, 3rd edition" section 13.5 on "Template
> Specialization" for raw pointers. Hereby you prevent a lot of code-bloat
> when using containers of many different pointer types, and it seems to
> me to be an important requirement for using smart pointers, that the
> same optimization can be used for them.
> I tried to patch boost/smart_ptr.hpp by adding a static_cast in the
> template constructor as shown below. This made the warning go away as I
> wanted.
> template<typename Y>
> shared_ptr(const shared_ptr<Y>& r) : px(static_cast<T*>(r.px)) {
> ++*(pn =;
> }
> Now my questions: Are there better ways to obtain what I want? If not:
> Are there any inherent problems in adding the above mentioned
> static_cast? And also: Should this static_cast also be added in other
> places?

Boost list run by bdawes at, gregod at, cpdaniel at, john at