I want to loop over (unsigned) integers from 0 to 100. For that I have 2 options - use size_t
, or auto
:
#include <typeinfo>
#include <iostream>
#ifndef use_auto
#include <cstddef>
#endif
int main(){
#ifdef use_auto
#ifdef suffix
constexpr auto num = 1;
#else
constexpr auto num = 1ul;
#endif
for (auto i = 0; i < num; i++){
std::cout << "auto\n";
}
#else
constexpr size_t num = 1;
for (size_t i = 0; i < num; i++){
std::cout << "explicit type\n";
}
#endif
std::cout << typeid(num).name() << '\n';
return 0;
}
$ g++ bla.cpp && ./a.out | c++filt -t
explicit type
unsigned long
$ g++ bla.cpp -Duse_auto -Dsuffix && ./a.out | c++filt -t
auto
int
$ g++ bla.cpp -Duse_auto && ./a.out | c++filt -t
auto
unsigned long
Now, the branch defined under "use_auto" + "suffix" could be considered the best, as it:
- uses auto
- gets the correct type due to using integer literal
My question is more of a "best practice". For me, size_t
seems clearer than auto num = 1ul
, but I do like using auto as much as possible, for readablity and maintainanability.
Is there a concrete guideline on what's best to use here, or is this opinion based?