Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Avoid calling Windows max() and min() macros #1053

Closed
wants to merge 3 commits into from

Conversation

jacobperron
Copy link
Member

@jacobperron jacobperron commented Apr 9, 2020

Previously Windows CI was failing: Build Status
With this change: Build Status

Signed-off-by: Jacob Perron <jacob@openrobotics.org>
@dirk-thomas
Copy link
Member

Do we have any idea why this started to happen? I think that would be essential to decide what the proper fix is.

@jacobperron
Copy link
Member Author

jacobperron commented Apr 9, 2020

Do we have any idea why this started to happen? I think that would be essential to decide what the proper fix is.

I don't know why it started to happen, but I tried reverting ros/pluginlib#186 and the connected PRs (also reverting ros/class_loader#139) and the failures went away. Here's a job I ran with all of the changes reverted: https://ci.ros2.org/job/ci_windows/9917

@jacobperron
Copy link
Member Author

jacobperron commented Apr 9, 2020

Regardless of how it started happening, this seems like a good, defensive, patch to me. If users include rclcpp headers into their code that also happens to include Windows.h then they will see the same issue.

@wjwwood
Copy link
Member

wjwwood commented Apr 9, 2020

If windows.h is included before NOMINMAX is defined, it will not fix the issue. Most likely something we did in those prs changed that, either introduced windows.h earlier or we removed what was previously defining NOMINMAX, e.g. poco's headers.

@dirk-thomas
Copy link
Member

I would suggest to try added the following before this newly added include https://github.com/ros2/rcutils/blob/f368c0f5b9666663f6c8e99b159221b4282d632c/include/rcutils/shared_library.h#L29:

#ifndef NOMINMAX
#define NOMINMAX
#endif

@wjwwood
Copy link
Member

wjwwood commented Apr 9, 2020

The parenthesis is still safer, because all someone has to do is this:

#include "windows.h"

// all of our headers

To break it again.

Use parentheses instead.

Signed-off-by: Jacob Perron <jacob@openrobotics.org>
@wjwwood
Copy link
Member

wjwwood commented Apr 9, 2020

I'd recommend doing both, merging ros2/rcutils#229 and doing something to protect ourselves, I'd recommend this:

// This "dummy" macro is used to break up uses of min and max to avoid
// the min/max macros that may be defined by windows.h from breaking things.
// See: ...issue or additional resources here
#define PREVENT_WINDOWS_MIN_MAX_MACRO  // could be shorter, I was being explicit

// ...

  return (y > 0) && (x > (std::numeric_limits<T>::max PREVENT_WINDOWS_MIN_MAX_MACRO () - y));

// ...

Duration
Duration::max PREVENT_WINDOWS_MIN_MAX_MACRO  ()
{
  return Duration(std::numeric_limits<int32_t>::max PREVENT_WINDOWS_MIN_MAX_MACRO  (), 999999999);
}

This approach was suggested here:

https://stackoverflow.com/a/11550864

@jacobperron
Copy link
Member Author

@wjwwood Why use a macro instead of parentheses?

Signed-off-by: Jacob Perron <jacob@openrobotics.org>
@jacobperron
Copy link
Member Author

Sadly, uncrustify doesn't like the parentheses (8dc3d6a).

@wjwwood
Copy link
Member

wjwwood commented Apr 9, 2020

@wjwwood Why use a macro instead of parentheses?

Because if I'm reading the code and see:

return Duration((std::numeric_limits<int32_t>::max)(), 999999999);

I think "that's weird, is that a mistake?" You could have a comment on each one, as you do in some places here, but if I see:

return Duration(std::numeric_limits<int32_t>::max PREVENT_WINDOWS_MIN_MAX_MACRO (), 999999999);

I think "What the heck is PREVENT_WINDOWS_MIN_MAX_MACRO?" and I go look it up, where I'll see some nice thorough comments about what it means.

But it's completely subjective.

@jacobperron
Copy link
Member Author

I think #1054 is a better alternative, leaving code more readable.

@jacobperron jacobperron deleted the jacob/avoid_max_min branch April 15, 2020 17:48
DensoADAS pushed a commit to DensoADAS/rclcpp that referenced this pull request Aug 5, 2022
Signed-off-by: Esteve Fernandez <esteve.fernandez@tier4.jp>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants