If I use a lambda-exppr in a method then I want to call another method then should I capture `this` by value or by reference?

2020-01-22 c++ foreach lambdaexpression

For educational reasons I've decided to implement a class that simulates std::string.

  • I have a method called append which I've implemented this way:

    template<class T, class U>
    inline String& String::append(T b, U e)
    {
        std::for_each(b, e, [this](char c) {push_back(c); }); // [&] or this?
    
        return *this;
    }
    

So I need to access this pointer inside the lambda-exppr so should I capture this by value or by reference? ** if by value then it works fine also.

Answers

Related