Hi, I was just testing and trying to figure out why this results in an error. I assume it has to do with how the compiler is storing the lambda argument 'self' but I can't figure out why it would make it const. I think lambda's are pretty much just turned into local structs with arguments as member variables. I want it to make a copy, using the copy constructor. I have a reason for that, this is trimmed. (Using 'this' can currently result in dangling pointers). Anything I could try?
Edit: it just hit me to check the GCC C++0x page, and this const feature isn't actually implemented yet... so it won't work. Thoughts and opinions are still welcome though (use of reference, etc.) :)
Code:class impl
{
public:
impl() { }
impl(impl& p) { }
void connecter()
{
impl self(*this); // trying to copy current object
boost::function<void()> on_connect = [self]() // trying to pass by value
{
self.something(); // error
// edit: this entire scope is const apparently
};
}
void something()
{
}
};
It works if I use a reference (ie. &self) but after the function scope ends, wouldn't 'self' be deleted, or something? Isn't that unsafe? Undefined behavior? Dangling reference?Quote:
error: passing ‘const impl’ as ‘this’ argument of ‘void impl::something()’ discards qualifiers|
It also works if I make something() const, but then I'd never be able to modify anything because I haven't solved the problem of why it's making it const to begin with.
Thanks in advance