If the lambda is using many variables from outer scope and we do not want to mention each one of them in capture clause, we can use default captures.

If lambda is using all variables by value, we can mention = in the capture clause.

int main(int argc, char const *argv[])
{
    int by_value{10};
    int by_ref{12};
 
    auto printAllByValue{
        [=]
        {
            std::cout << by_value << '\n';
            std::cout << by_ref << '\n';
        }};
    return 0;
}

If lambda is using all variables by reference, we can mention & in the capture clause.

int main(int argc, char const *argv[])
{
    int by_value{10};
    int by_ref{12};
 
    auto printAllByRef{
        [&]
        {
            std::cout << by_value << '\n';
            std::cout << by_ref << '\n';
        }};
    return 0;
}

Mixing default captures and specific capture

It is also possible to use default captures for lets say capture by value and specific variables by reference or vice versa. For example:

int main(int argc, char const *argv[])
{
    int by_value{10};
    int by_ref{12};
 
    auto printOneByRefOthersByValue{
        [=, &by_ref]
        {
            std::cout << by_value << '\n';
            std::cout << by_ref << '\n';
        }};
    return 0;
}

Just note that, the default capture should be first in the list. Furthermore, there should not be duplicate captures. For example, following will raise compilation error:

int main(int argc, char const *argv[])
{
    int by_value{10};
    int by_ref{12};
 
    auto invalid1{
        [by_value, by_value] // duplicates
        {
            std::cout << by_value << '\n';
            std::cout << by_ref << '\n';
        }};
    auto invalid2{
        [=, by_value] // `=` already told to capture everything by value
        {
            std::cout << by_value << '\n';
            std::cout << by_ref << '\n';
        }};
 
    auto invalid3{
        [&by_ref, &by_ref] // duplicates
        {
            std::cout << by_value << '\n';
            std::cout << by_ref << '\n';
        }};
    auto invalid4{
        [&, &by_ref] // '&' already told to capture everything by ref
        {
            std::cout << by_value << '\n';
            std::cout << by_ref << '\n';
        }};
    
    return 0;
}

References

  1. https://www.learncpp.com/cpp-tutorial/lambda-captures/