Lambdas Require Capturing 'This' to Call Static Member Function

lambdas require capturing 'this' to call static member function?

I agree, it should compile just fine. For the fix (if you didn't know already), just add the reference capture and it will compile fine on gcc 4.6

struct B
{
void g()
{
[&]() { B::f(); }();
}

static void f() { std::cout << "Hello World" << std::endl; };
};

use c++11 lambda in non-static member function and call static member function of same class

class A {
static void a() {}
public:
void x() {
[] () { A::a(); }();
}
};

int main() { A a; a.x(); }

This compiles fine with clang 3.2. Also, there is no reason why this shouldn't be compile. This is a bug in GCC as per the notes. This ought to be fixed in 4.7.1. Note that gcc started early but has lagged behind clang in supporting some of the C++11 features.

Edit: There is a similar SO question which you may want to check out.

Capturing static member variable inside lambda function

Static storage variables such as static members do not need to be captured. Simply remove the capture and it will work. You don't need to qualify the scope either, since you're in a member function.

How does lambda capture local static variable?

You don't need to capture global or static variables. Only automatic variables have to be captured if you want to use them.

Because of that, yes, you can convert such lambda to a function pointer.


Cppreference:

The identifier in any capture without an initializer (other than the this-capture) is looked up using usual unqualified name lookup in the reaching scope of the lambda. The result of the lookup must be a variable with automatic storage duration declared in the reaching scope.

(emphasis mine)

"To capture" means to put a copy or a reference to a variable into a lambda object itself.

Global and static variables have fixed memory location. You don't need to store copies or references to them to use them since their location is known at compile time and doesn't change.

(If you really want to have a copy of a global or static variable, you can create it using named capture from C++14, which lets you create a custom member variable in a lambda object. But such capture is not necessary if you just want to use that global / static variable.)

Automatic variables, on the other hand, don't have a fixed location. Multiple lambdas created at the exact same place could refer to different automatic variables. Because of that, a lambda object has to capture such variable - which means to contain either a reference to used automatic variable or a copy of it.

Capturing this required to access member functions?

GCC is right: to call member functions on this from inside a lambda, you must capture this. If the member function does not depend on the data members, make it static, and then you do not need to capture this.

You cannot capture "functions", only local variables (including parameters and this, but not specific data members).

C++17 decorate lambda or member function with non-capturing lambda

First, your decorate_lambda is buggy: it silently breaks if you call it with a stateful callable. As a simple check, you could allow callables only if std::is_empty_v is true.



The end result that I would like is to roll decorate_lambda and decorate_memfn into a single decorate(lambda_or_memfn) function

You can use std::integral_constant

template<auto x>
inline constexpr std::integral_constant<decltype(x), x> constant{};

template<typename T, typename F, F T::* x>
auto decorate(std::integral_constant<F T::*, x>)
{
return [](auto&&... args) {
return 100 + std::mem_fn(x)(decltype(args)(args)...);
};
}

auto d = decorate(constant<&Worker::work>);

C++ Lambda: Access static method in lambda leads to error 'this was not captured for this lambda function'

The following work-around works:

template< typename T > T* global_instance() { return T::instance(); }

void(*func_pointer)(void) = []{
global_instance<Singleton>()->bar();
};


Related Topics



Leave a reply



Submit