jeudi 30 août 2018

C++: Almost Always Auto and for loops with counter

Herb Sutter states Almost Always Auto and I have the following code:

use count_t = int;
count_t get_count() { ... };

const auto count = get_count();
for (decltype(count) i = 0; i < count; i++) {
    // Do the stuff
}

Essentially, using decltype() allows me to write a for loop that may use any integer type (hoping that get_count() would never return floating point) without any modifications in a client code of get_count() function and avoid compilation warnings like "signed-unsigned" mismatch.

My question is: is this form acceptable at all in an assumption that count_t may be redefined in future?

Aucun commentaire:

Enregistrer un commentaire