Reputation: 61
I want to learn how to make compact switches, tell me if I'm doing it right or can I simplify it?
auto _time = 5s;
bool save_time;
auto fs_time = steady_clock::now();
for(;;) {
auto now_time = steady_clock::now();
if (duration_cast<seconds>(now_time - fs_time) >= _time) {
save_time = true;
}
else {
save_time = false;
}
// CODE ....
if(save_time) {
// CODE ....
}
if(save_time) {
// CODE 2 ....
}
}
I do this to not write the same thing repeatedly.
if (duration_cast<seconds>(now_time - fs_time) >= _time) {}
Perhaps this slows down the code when it checks it constantly.
Upvotes: 1
Views: 155
Reputation: 3460
Compact means in one line?
save_time = (duration_cast<std::chrono::seconds>(now_time - fs_time) >= _time) ? true : false;
Or even more compact as Default suggested:
save_time = (duration_cast<std::chrono::seconds>(now_time - fs_time) >= _time);
Regarding the use of using, have a look to this question: Why is "using namespace std;" considered bad practice?
Upvotes: 4