Boost::Signal inside a std::map
Recently, I came across a problem with Boost::Signal. I wanted to use
them inside a std::map
so I could map signals to categories, and this
turned out to be not as simple as one might think.
Problem
The main problem is that boost::signal
is noncopyable
, meaning you can’t use the assignment operator “=
” at all. This prevents the straightforward use in any STL container, as those depend on the assignment operator. You can of course have explicit instances of boost::signal
, but you can’t use them directly in a container, let alone add and remove them dynamically.
Solution
The first solution I tried was to store pointers (in my case, boost::shared_ptr<>
) to the signals, and fill the map with those. This works, but it is a bit cumbersome to maintain all the pointers and have new
all around the place. Fortunately, another boost library comes to the rescue, boost::ptr_map
from the Boost Pointer Container library. It has two specialities I came across (using boost 1.33.1 beta from 8th
November).
- Its
insert ()
function takes a non-const reference as key, unlike the std::map`. Be aware of that, as it can lead to hard-to-find compiler errors. - The dereferencing was non-standard, in my case I had to use
(iterator_to_ptr_map)(params_for_signal)
, I supposed I would have to add some “*
” somewhere but somehow the map didn’t need it.
Will try with Boost 1.33.1 final and report if there is any difference to the beta.