Boost logo

Boost :

From: Lars Thorup (lars.thorup_at_[hidden])
Date: 2000-10-01 14:01:30


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

   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

Lars Thorup

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