PHPFixing
  • Privacy Policy
  • TOS
  • Ask Question
  • Contact Us
  • Home
  • PHP
  • Programming
  • SQL Injection
  • Web3.0

Thursday, November 3, 2022

[FIXED] Why is a C++ lambda life cycle bound to the smallest block scope?

 November 03, 2022     c++, lambda, static     No comments   

Issue

In the following example program, I would expect the counter to reset at the beginning of each cycle:

#include <iostream>
#include <functional>

void run_lambda(std::function<void()> fun){ fun(); fun(); }
int main(){
    for(int i = 0; i < 3 ; ++i){
        run_lambda([i](){
            static int testVal = 0;
            std::cout << "value[" << i << "]:" << testVal << std::endl;
            ++testVal;
        });
    }
    return 0;
}

Instead the output is:

value[0]:0
value[0]:1
value[1]:2
value[1]:3
value[2]:4
value[2]:5

According to this answer:

The closure type is declared in the smallest block scope, class scope, or namespace scope that contains the corresponding lambda-expression.

In my understanding this means, that the lambda object life cycle is bound to the loops block scope, thus the static variable is not resetting at the start of the loop.

Is there a specific reason for this? Intuitively one would think that a lambda expression is essentially a temporary object, and as such in this example it would cease to exist after the statement.


Solution

Your code is basically equivalent to:

#include <iostream>
#include <functional>

struct lambda
{
  lambda(int i): i(i) {}
  int i;
  void operator()()
  {
    static int testVal = 0;
    std::cout << "value[" << i << "]:" << testVal << std::endl;
    ++testVal;
  }
};

void run_lambda(std::function<void()> fun){ fun(); fun(); }
int main(){
    for(int i = 0; i < 3 ; ++i){
        lambda l{i};
        run_lambda(l);
    }
    return 0;
}

Whilst each iteration of the loop does indeed create a new lambda object each object is an instance of the same class and therefore shares the same static variables.

The statement you're quoting is saying that the type is declared inside the scope where the lambda is declared. It isn't saying that each time the code enters that scope a new type is created (c++ doesn't have dynamic types like that).



Answered By - Alan Birtles
Answer Checked By - Pedro (PHPFixing Volunteer)
  • Share This:  
  •  Facebook
  •  Twitter
  •  Stumble
  •  Digg
Newer Post Older Post Home

0 Comments:

Post a Comment

Note: Only a member of this blog may post a comment.

Total Pageviews

Featured Post

Why Learn PHP Programming

Why Learn PHP Programming A widely-used open source scripting language PHP is one of the most popular programming languages in the world. It...

Subscribe To

Posts
Atom
Posts
Comments
Atom
Comments

Copyright © PHPFixing