The following dummy example may not really make sense in real world. But it explains the question. I have a class Foo with members firstname and lastname. The function ForEachMessage takes a lambda. I want it to capture only the firstname of Foo but not lastname. How do I achieve that?
#include <iostream>
#include <vector>
#include <functional>
using namespace std;
vector<string> messagesList;
void ForEachMessage(function<void(const string&)>callBack)
{
    for (const auto& str : messagesList) {
        callBack(str);
    }
}
class Foo {
public:
    std::string firstname;
    std::string lastname;
    void say() {
        ForEachMessage([this](const std::string& someMessage)
        {
            cout << firstname << ": " <<  someMessage << endl;
        });
        // Error: firstname in capture list doesn't name a variable
        // ForEachMessage([firstname](const std::string& someMessage)
        // {
        //    cout << firstname << ": " <<  someMessage << endl;
        // });
        // Error: expect ',' or ']' in lambda capture list
        // ForEachMessage([this->firstname](const std::string& someMessage)
        // {
        //    cout << firstname << ": " <<  someMessage << endl;
        // });
    }
};
int main(int argc, const char * argv[]) {
    Foo foo;
    foo.firstname = "Yuchen";
    foo.lastname = "Zhong";
    messagesList.push_back("Hello, World!");
    messagesList.push_back("Byebye, World!");
    foo.say();
    return 0;
}
 
     
     
    