sharkdp / dbg-macro

A dbg(…) macro for C++
MIT License
2.97k stars 257 forks source link

Prefer bitsize variant over standard integral types #128

Closed winwinashwin closed 1 year ago

winwinashwin commented 1 year ago

Fixes #69

Have adapted some tests, replaced some types in const, pointer tests to keep code clean.

Note : Although the CI checks are all passing, the standard does not mandate fixed width integer types to be available. Though very rare, there might be cases when these are undefined (embedded applications is what I can think of). I am not sure if if this project in intended to support such applications. If that is the case we could wrap each association with a feature test macro. Something like

#if defined(INT16_MAX)
DBG_MACRO_REGISTER_TYPE_ASSOC(short, int16_t)
#else 
DBG_MACRO_REGISTER_TYPE_ASSOC(short, short)
#endif 

Or if you have any other elegant suggestions please let me know!

sharkdp commented 1 year ago

Note : Although the CI checks are all passing, the standard does not mandate fixed width integer types to be available. Though very rare, there might be cases when these are undefined (embedded applications is what I can think of). I am not sure if if this project in intended to support such applications.

Thank you for thinking about this. I think it's fine to rely on the availability. For now, we do support the platforms and compilers that are tested in CI (GCC, Clang, MSVC). If someone wants to add support for additional platforms or compilers, we can re-evaluate this.

winwinashwin commented 1 year ago

Another behaviour to note, even though the associations are registered, type names deduced from __PRETTY_FUNCTION__ and friends will still show int instead of int32_t. See below

int var{};
dbg(var);  // var = 0 (int32_t)

int arr1[3]{};
dbg(arr1);  // arr1 = {0, 0, 0} (int [3])

std::array<int, 3> arr2{};
dbg(arr2);  // arr2 = {0, 0, 0} (std::array<int, 3>)

std::array<std::pair<int, int>, 3> arr3{};
dbg(arr3);  // arr3 = {{0, 0}, {0, 0}, {0, 0}} (std::array<std::pair<int, int>, 3>)
dbg(arr3[0]);  // arr3[0] = {0, 0} (std::pair<int32_t, int32_t>&)

I am not sure how to fix this apart from specialising get_type_name for such common cases. What are your thoughts on this?

sharkdp commented 1 year ago

Thank you. This looks (almost) good to go. Can you please remove the tests/example binary?

I am not sure how to fix this apart from specialising get_type_name for such common cases. What are your thoughts on this?

Hm. Adding a specialization for C-style arrays and std::array would probably be a good idea, to avoid inconsistencies/confusion. But feel free to skip this for now.

winwinashwin commented 1 year ago

Love this project! Taught me a lot :smile: