lundi 20 mai 2019

Skip Iteration in std::for_each on specific condition

I'm working on a program that needs to iterate over a range. I wish to know if I can use continue like when I use in range based for loop.

Working :

for (auto i : *plc_list)
{
    std::optional<std::vector<config_table_struct>> config_list = db.get_config(plc_ip);
    if (not config_list.has_value()) {
        //Display and Log Error Message 1
        continue; //Skip current iteration
    }
    if (config_list->empty()) {
        //Display and Log Error Message 2
        //Do Some-Thing
        continue; //Skip current iteration
    }
}

Not-Working :

std::optional<std::vector<std::string>>         plc_list = db.get_plc_list();
std::for_each(plc_list->begin(), plc_list->end(), [&db, &plc](const std::string& plc_ip) {
    db.create_table(plc_ip);
    std::optional<std::vector<config_table_struct>> config_list = db.get_config(plc_ip);
    if (not config_list.has_value()) {
        //Display and Log Error Message 1
        //continue; //Skip current iteration
    }
    if (config_list->empty()) {
        //Display and Log Error Message 2
        //Do Some-Thing
        //continue; //Skip current iteration
    }
    //Perform Normal Operations
}

Aucun commentaire:

Enregistrer un commentaire