xelatihy / yocto-gl

Yocto/GL: Tiny C++ Libraries for Data-Driven Physically-based Graphics
https://xelatihy.github.io/yocto-gl
2.83k stars 207 forks source link

Use std::ssize() instead of .size() in all loops? #1313

Closed xelatihy closed 2 years ago

xelatihy commented 2 years ago

C++ containers have the notorious signed/unsigned problem. Through Yocto indices are sometimes signed, when explicitly supporting 64bit, or signed, when 32bit is required. On GCC/clang default settings this works fine, since no warnings are issued when comparing signed and unsigned values. On MSVC, this does not work as well.

One possibility is to pedantically use unsigned everywhere, but that is problematic in other manners, in particular since unsigned comparisons are prone to error due to wrapping.

The other option is to always use signed comparison, which may mean switching to signed std::ssize(). We should investigate this option and see if it solves the issue.

xelatihy commented 2 years ago

To implement this, we will wait to C++20 availability.

xelatihy commented 2 years ago

As an added option, just use as much as possible the Python-like range and enumerate iterators.

xelatihy commented 2 years ago

Partially fixed in #1326