问题
I want to save boost signals objects in the map (association: signal name → signal object). The signals signature is different, so the second type of map should be boost::any.
map<string, any> mSignalAssociation;
The question is how to store objects without defining type of new signal signature?
typedef boost::signals2::signal<void (int KeyCode)> sigKeyPressed;
mSignalAssociation.insert(make_pair("KeyPressed", sigKeyPressed()));
// This is what I need: passing object without type definition
mSignalAssociation["KeyPressed"] = (typename boost::signals2::signal<void (int KeyCode)>());
// One more trying which won't work. And I don't want use this
sigKeyPressed mKeyPressed;
mSignalAssociation["KeyPressed"] = mKeyPressed;
All this tryings throw the error:
/usr/include/boost/noncopyable.hpp: In copy constructor ‘boost::signals2::signal_base::signal_base(const boost::signals2::signal_base&)’:
In file included from /usr/include/boost/signals2/detail/signals_common.hpp:17:0,
/usr/include/boost/noncopyable.hpp:27:7: error: ‘boost::noncopyable_::noncopyable::noncopyable(const boost::noncopyable_::noncopyable&)’ is private
/usr/include/boost/signals2/signal_base.hpp:22:5: error: within this context
----------
/usr/include/boost/signals2/detail/signal_template.hpp: In copy constructor ‘boost::signals2::signal1<void, int&, boost::signals2::optional_last_value<void>, int, std::less<int>, boost::function<void(int)>, boost::function<void(const boost::signals2::connection&, int)>, boost::signals2::mutex>::signal1(const boost::signals2::signal1<void, int, boost::signals2::optional_last_value<void>, int, std::less<int>, boost::function<void(int)>, boost::function<void(const boost::signals2::connection&, int)>, boost::signals2::mutex>&)’:
In file included from /usr/include/boost/preprocessor/iteration/detail/iter/forward1.hpp:52:0,
/usr/include/boost/signals2/detail/signal_template.hpp:578:5: note: synthesized method ‘boost::signals2::signal_base::signal_base(const boost::signals2::signal_base&)’ first required here
from /usr/include/boost/signals2.hpp:16,
---------
/usr/include/boost/signals2/preprocessed_signal.hpp: In copy constructor ‘boost::signals2::signal<void(int)>::signal(const boost::signals2::signal<void(int)>&)’:
In file included from /usr/include/boost/signals2/signal.hpp:36:0,
/usr/include/boost/signals2/preprocessed_signal.hpp:42:5: note: synthesized method ‘boost::signals2::signal1<void, int, boost::signals2::optional_last_value<void>, int, std::less<int>, boost::function<void(int)>, boost::function<void(const boost::signals2::connection&, int)>, boost::signals2::mutex>::signal1(const boost::signals2::signal1<void, int, boost::signals2::optional_last_value<void>, int, std::less<int>, boost::function<void(int)>, boost::function<void(const boost::signals2::connection&, int)>, boost::signals2::mutex>&)’ first required here
from /home/ockonal/Workspace/Projects/Pseudoform-2/include/Core/Systems.hpp:6,
回答1:
This has nothing to do with any
or map
. Boost signals are simply non-copyable. You can wrap them in a smart pointer such as shared_ptr
if you want something that's copyable and cleans up after itself.
来源:https://stackoverflow.com/questions/3057009/storing-objects-in-the-array