TommyKaneko / Sketchup-API-C-Wrapper

A complete set of C++ Wrapper classes for SketchUp C API objects
MIT License
29 stars 8 forks source link

[[maybe_unused]] an alternative to _unused #57

Open thomthom opened 1 year ago

thomthom commented 1 year ago

Not sure what this project's C++ version policy is, but since C++17 we have [[maybe_unused]] that can suppress unused variable warnings.

https://github.com/TommyKaneko/Sketchup-API-C-Wrapper/blob/6879346ce7ca292962e40246e4f587020fcc9273/src/SUAPI-CppWrapper/model/TypedValue.cpp#L28-L29

TommyKaneko commented 1 year ago

Ah, this isn't for suppressing warnings. It's been a while when I did this, but (I think) it served the purpose of easily removing variables that were created for assert() checks, for production builds. I need the asserts for debugging.

thomthom commented 1 year ago

How is this removing any variable?

Looking at how it's used:

https://github.com/TommyKaneko/Sketchup-API-C-Wrapper/blob/6879346ce7ca292962e40246e4f587020fcc9273/src/SUAPI-CppWrapper/model/TypedValue.cpp#L144-L157

The results variable is created regardless. But in Release it'll be unused and produce a warning.

This should do the same, but with newer C++ semantics.

SUTypedValueRef TypedValue::create_typed_value() {
  SUTypedValueRef typed_value = SU_INVALID;
  [[maybe_unused]] auto res = SUTypedValueCreate(&typed_value);
  assert(res == SU_ERROR_NONE);
  return typed_value;
}
TommyKaneko commented 1 year ago

OK, thank for the tip. I will implement that. The wrapper was made initially with C++14, so I will get on and make those changes.

thomthom commented 1 year ago

I've been recently migrating my projects to C++20 and it's adding quite a few nice features. Even C++17 add a good chunk of useful improvements to the language.