Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Lambda Capture Variable Not Used in Generated Code #640

Open
glucu opened this issue May 17, 2024 · 2 comments
Open

Lambda Capture Variable Not Used in Generated Code #640

glucu opened this issue May 17, 2024 · 2 comments

Comments

@glucu
Copy link

glucu commented May 17, 2024

Issue:
The generated code for a lambda function that captures a variable by value does not use the captured variable in the lambda body.

Example Code:

int main()
{
    int z{5};
    auto sum = [z](int x, int y){ return x + y + z; };
}

Generated Code

int main()
{
  int z = {5};
    
  class __lambda_7_16
  {
    public: 
    inline /*constexpr */ int operator()(int x, int y) const
    {
      return (x + y) + x;
    }
    
    private: 
    int z;
    
    public:
    __lambda_7_16(int & _z)
    : z{_z}
    {}
    
  };
  
  __lambda_7_16 sum = __lambda_7_16{z};
  return 0;
}

Expected Generated Code

int main()
{
  int z = {5};
    
  class __lambda_7_16
  {
    public: 
    inline /*constexpr */ int operator()(int x, int y) const
    {
      return (x + y) + z; // Correct: `z` is used here 
    }
    
    private: 
    int z;
    
    public:
    __lambda_7_16(int & _z)
    : z{_z}
    {}
    
  };
  
  __lambda_7_16 sum = __lambda_7_16{z};
  return 0;
}

Note
The issue seems to be intermittent, and it may not reproduce consistently.

@andreasfertig
Copy link
Owner

Hello @glucu,

thanks for reporting this issue! After a couple of days and tries, I was not able to reproduce it. Are you running a self-build version?

Andreas

@glucu
Copy link
Author

glucu commented May 30, 2024

Hi @andreasfertig

I also tested further and was unable to reproduce the problem. Odd. This was run on cppinsights.io.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants