If I capture the "this"-ptr in a lambda I can call member functions without problems. However, when I instead capture the pointer explicitely (without mentioning "this"), it stops working. Am I doing something wrong? According to my understanding, the pointers should be the very same, so this really surprised me. Is there some compiler magic going on to treat "this" in a special way?
#include <cstdio>
#include <string>
struct client
{
auto foo(std::string&& other)
{
printf("%s!\n", other.data());
}
void local()
{
std::string str = "Hello World this is a sentence to long for sso!";
auto lambda = [this, other = std::move(str)]() mutable {
foo(std::move(other));
}();
}
static auto external(void* ptr) {
std::string str = "Hello World this is a sentence to long for sso!";
client* conv_ptr = static_cast<client*>(ptr);
auto lambda = [conv_ptr, other = std::move(str)]() mutable {
foo(std::move(other));
}();
}
};
int main()
{
client c1;
c1.local();
client::external(static_cast<void*>(&c1));
}
Yields:
<source>:15:14: error: 'void lambda' has incomplete type
15 | auto lambda = [this, other = std::move(str)]() mutable {
| ^~~~~~
<source>: In lambda function:
<source>:25:16: error: cannot call member function 'auto client::foo(std::string&&)' without object
25 | foo(std::move(other));
| ~~~^~~~~~~~~~~~~~~~~~