Boost Cookbook

Latest recipes

Atom feed of new recipes and blog entries (only recipes)

Composable natural sort
By Kirit — 15th December, 2007 04:26 (UTC)
Thread-safe lazy singleton template class
By Port4l — 2nd December, 2007 05:53 (UTC)
Template function to create a boost::shared_ptr
By bobbyrward — 28th September, 2007 04:51 (UTC)
Binary integral literals with template metaprogramming
By dizzy — 26th September, 2007 09:20 (UTC)
Regular expressions for UK postcodes
By Kirit — 24th September, 2007 14:46 (UTC)
Worker threads returning futures
By Kirit — 24th September, 2007 11:46 (UTC)
Function composition
By Kirit — 23rd September, 2007 12:59 (UTC)

From the forums

Latest three forum posts (reverse order)

How to get working with covariant shared_ptr trick? dataangel 4th March, 2008 09:18 (UTC)

Kirit said

dataangel said

The problem I'm having is that with new_ptr the wrapper function can't make a shared_ptr from the virtual function's raw pointer anymore: the compiler complains ~MyClass() is private (as new_ptr's usage suggests it should be).

The private constructors and destructor is there to ensure that objects of that type cannot be constructed on the stack. If that isn't relevant then you wouldn't do that.

As for the compiler's complaint, is there a reason you don't just extend friendship to the wrapper function?

Sorry I wasn't clear — the complaint from the compiler isn't that the wrapper function doesn't have permission. The wrapper function is already part of the class so AFAICT friendship wouldn't make a difference. The problem is that boost::shared_ptr's internals don't have access to the destructor. Looking at the new_ptr code, it looks like your trick is to pass the shared_ptr constructor an alternative deallocator, so it doesn't have this problem. But when the wrapper function wants to make a shared_ptr out of the raw pointer it already has, it can't use new_ptr (because the object has already been allocated), nor does it have access to new_ptr's deallocator. I guess I could reproduce the trick with my class having it's own new_ptr-like deallocate function, but that's getting awfully hacky…

How to get working with covariant shared_ptr trick? Kirit 2nd March, 2008 06:40 (UTC)

dataangel said

The problem I'm having is that with new_ptr the wrapper function can't make a shared_ptr from the virtual function's raw pointer anymore: the compiler complains ~MyClass() is private (as new_ptr's usage suggests it should be).

The private constructors and destructor is there to ensure that objects of that type cannot be constructed on the stack. If that isn't relevant then you wouldn't do that.

As for the compiler's complaint, is there a reason you don't just extend friendship to the wrapper function?

OpenHow to get working with covariant shared_ptr trick? dataangel 1st March, 2008 04:00 (UTC)

One problem with using shared_ptr's in general is that covariant return types don't work for them. I have a class I'd like to have a Clone() member function. When B inherits from A, then A* and B* are covariant but shared_ptr<A> and shared_ptr<B> are not. There's a trick for getting around this documented here:

http://lists.boost.org/boost-users/2003/02/2996.php

His syntax is slightly wrong (you pass the raw cast pointer to the shared_ptr's constructor rather than assigning) but you get the basic idea. You have a protected virtual function that uses raw covariant pointers and a public function that uses it and wraps the result in a shared_ptr. The problem I'm having is that with new_ptr the wrapper function can't make a shared_ptr from the virtual function's raw pointer anymore: the compiler complains ~MyClass() is private (as new_ptr's usage suggests it should be).

Can anyone think of a way to make new_ptr workable somehow with covariant returns? Other than that I love it :)

Welcome to Boost cookbook (alpha). From an idea by dhruva and spec from Steve Toledo-Brown. Alpha version written by Kirit over a weekend. And yes, the site is nearly all C++ and uses Boost.

Latest blog post

Boost 1.36 released

Earlier today Beman Dawes announced the release of Boost 1.36. It contains 4 new libraries and updates to many others.

  • Accumulators: Framework for incremental calculation, and collection of statistical accumulators, from Eric Niebler.
  • Exception: A library for transporting of arbitrary data in exception objects, and transporting of exceptions between threads, from Emil Dotchevski.
  • Units: Zero-overhead dimensional analysis and unit/quantity manipulation and conversion, from Matthias Schabel and Steven Watanabe
  • Unordered: Unordered associative containers, from Daniel James.

Updated libraries include Asio, Assign, Function, Hash, Interprocess, Intrusive, Math, MPI, Multi-index Containers, PtrContainer, Spirit, Thread, Wave, and Xpressive.

It is available for download now on Sourceforge and should be on the Boost web site in a few hours.

Subversion check out is at

http://svn.boost.org/svn/boost/tags/release/Boost_1_36_0/

Kirit, 15th August, 2008 05:20 (UTC)

Discussion for this page


This is not an official Boost site. For more information on Boost please see Boost.org.