vendredi 14 juillet 2023

C++ Lambdas in return statements

Trying out C++ Lambdas. Since lambdas are expressions, they should work with return statements. But obv I was wrong.

My code is:

std::list<char>                                     all_cities;
std::list<std::pair<std::vector<char>, int>>        all_paths_list;

//stuff that doesn't matter

int size = all_cities.size();
all_paths_list.resize( return ( [&size] () -> int {
    size--;
    int fact = 1;
    while (x || 0)
        fact *= x--;

    return fact;
}));

Why isn't this working? Do lambdas HAVE to be given as args to functions that take a functor/lambda? Are there ways to make this work?

Keep in mind that all my code is inside a class, the code above (starting from line 6) is in the constructor.

Aucun commentaire:

Enregistrer un commentaire