class Whatever
{
public:
virtual ~Whatever();
protected:
Whatever();
virtual void SomeMethod();
void OnEventOccurred(int x);
std::vector<boost::signals2::scoped_connection> boostSignalConnections_;
}
// .cpp
Whatever::SomeMethod()
{
...
boostSignalConnections_.push_back(
anObject->AddEventOccurredObserver(
std::bind(&Whatever::OnEventOccurred,
this, std::placeholders::_1)));
...
}
(Note AddEventOccurredObserver just delegates to boost::signals2::connect() and returns the boost::signals2::connection)
I’m getting the following error. It’s hard to interpret template errors but it seems like the error is being caused at the member declaration of std::vector<boost::signals2::scoped_connection> boostSignalConnections_;
...\vc\include\xmemory(202): error C2248:
'boost::signals2::scoped_connection::scoped_connection' :
cannot access private member declared in class 'boost::signals2::scoped_connection'
...\boost_1_47\boost\signals2\connection.hpp(234) : see declaration of
'boost::signals2::scoped_connection::scoped_connection'
...\boost_1_47\boost\signals2\connection.hpp(210) :
see declaration of 'boost::signals2::scoped_connection'
...\vc\include\xmemory(201) : while compiling class template member function
'void std::allocator<_Ty>::construct(boost::signals2::scoped_connection *,_Ty &&)'
with
[
_Ty=boost::signals2::scoped_connection
]
I’ve searched and I think it might have to do with scoped_connection not being copyable? I’m not sure. The reason I’m using a scoped_connection is because of this SO question:
Handle connection/disconnection of many signals/slots with boost::signals2
Update
FYI, when I change from scoped_connection to connection it works: std::vector<boost::signals2::connection> boostSignalConnections_;. My whole reason for using scoped_connection is that they automatically disconnect the connection on destruction whereas I believe a connection doesn’t not. However, I can just iterate over the collection and manually disconnect each one.
boost::signals2::scoped_connectionis non-copyable, and in C++03,std::vector<T>requires thatTbe both copy-constructible and copy-assignable (this requirement is relaxed in C++11, where a non-copyable type is fine as long as it’s movable instead).The naive solution would be to make
boostSignalConnections_astd::vector<scoped_connection*>instead, but this introduces object lifetime and exception-safety issues that aren’t worth the effort. Instead, depending on how recent your compiler is, I’d recommend the following:boost::ptr_vector<scoped_connection>, which will store pointers for you (allowing non-copyable types) but give you value-semantics, and eliminate the aforementioned object lifetime and exception-safety issues.std::vector<std::unique_ptr<scoped_connection>>.That being said, why used
scoped_connectionif you don’t want your object to be lexically-scoped?