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 by value for reference typed variables. #64

Closed
textshell opened this issue Aug 28, 2018 · 2 comments
Closed

Lambda Capture by value for reference typed variables. #64

textshell opened this issue Aug 28, 2018 · 2 comments
Labels
bug Something isn't working

Comments

@textshell
Copy link

According to http://eel.is/c++draft/expr.prim.lambda#capture-10 and cppreference.org lambdas seems to capture references as copies of the decayed type.

But cppinsights does not agree given:

#include <string>

void func(const std::string& arg) {
 
  [arg] {
    return arg.size();
  };
}

int main() {
  std::string b;
  
  func(b);
}

It generates:

    const std::basic_string<char, std::char_traits<char>, std::allocator<char> > & arg;
    
    public: __lambda_5_3(const std::basic_string<char, std::char_traits<char>, std::allocator<char> > & _arg)
    : arg{_arg}
    {}

Where i would expect based on the linked section that it would produce:

    const std::basic_string<char, std::char_traits<char>, std::allocator<char> > arg;
andreasfertig added a commit that referenced this issue Aug 28, 2018
Fixed #64: Implicit lambda captures are captured by copy.
@andreasfertig andreasfertig added the bug Something isn't working label Aug 28, 2018
@andreasfertig
Copy link
Owner

Thanks for reporting that. Please check if it is correct now.

@textshell
Copy link
Author

Thanks. Now output matches my expectations and a small test program also agrees that by value captures of references don't allow modifying the original object.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

No branches or pull requests

2 participants