This question already has an answer here:
- Passing lambda as function pointer 2 answers
 
I'm quite proficient with C# but I'm new to C++ and I have really newbie question so please don't judge me rough
I have the following function pointer type:
typedef double (*PenaltyDelegate)(lifealgo *algo, int row, int col);
I thought that function pointer is the same thing as lambda independent of closure variables, but it turns out as soon as I specify variables that have to be catured or use one of generic capture modes, such as [=] or [&] the following assign stops working:
...
PenaltyDelegate penalty = [penalty_when_table_rows, penalty_when_table_cols, penalty_amount, penalty_when_table] (lifealgo *algo, int row, int col) -> double {
    auto index = 0;
    auto found = true;
    auto when_table = *penalty_when_table;
    for (int r = 0; found && r < penalty_when_table_rows; r++) {
        auto rr = r + row;
        for (int c = 0; found && c < penalty_when_table_cols; c++) {
            auto cell = 1 << algo->getcell(rr, col + c);
            auto when = when_table[index++];
            if ((cell & when) == 0) found = false;
        }
    }
    if (found) {
        return penalty_amount;
    } else {
        return 0.0;
    }
};
...
What is the reason for that to happen? Thank you in advance!
Aucun commentaire:
Enregistrer un commentaire