As I understand it, scoped_connection
s are meant to automatically disconnect when they go out of scope, and not before. I've found that this doesn't work correctly when the scoped_connection is in std::vector
(and it probably fails for other containers as well).
eg.
using boost::signals::scoped_connection; // just for readability
boost::signal<void ()> sig;
std::vector<scoped_connection> connection_vec;
connection_vec.push_back(sig.connect(foo));
assert(connection_vec.back().connected()); // this assertion fails!
I've seen this explained by claiming that std::vector
requires that the elements be copyable whereas scoped_connection
is uncopyable - but that's not entirely true. std::vector
only requires that the elements be moveable. (And I expect scoped_connection
should be moveable.)
For example, the following code works:
std::vector<std::unique_ptr<scoped_connection>> vec2;
// Note that std::unique_ptr is uncopyable, but moveable
vec2.push_back(std::unique_ptr<scoped_connection>(new scoped_connection(sig.connect(foo))));
assert((*vec2.back()).connected()); // this assertion succeeds!
Besides, if the problem was due to std::vector
trying to copy an uncopyable thing, then shouldn't that produce a compiling error?
So I'm wondering what is the actual reason for scoped_connection
not working correctly inside a vector
, and is this a problem that can be fixed within boost.
(I tested this on gcc version 4.8.0 with boost version 1.53.0.)
scoped_connection has no move constructor (yet). So it is not movable as you assume, instead
std::move
will fallback to copy. And as you mentioned you cannot copy around ascoped_connection
.