Skip to content

refactor: work around sign compare warning #551

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

Merged
merged 1 commit into from
Aug 11, 2023
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 5 additions & 4 deletions include/cpp2util.h
Original file line number Diff line number Diff line change
Expand Up @@ -398,15 +398,16 @@ auto assert_not_null(auto&& p CPP2_SOURCE_LOCATION_PARAM_WITH_DEFAULT) -> declty
//
auto assert_in_bounds(auto&& x, auto&& arg CPP2_SOURCE_LOCATION_PARAM_WITH_DEFAULT) -> decltype(auto)
requires (std::is_integral_v<CPP2_TYPEOF(arg)> &&
requires { std::ssize(x); x[arg]; std::begin(x) + 2; })
requires { std::size(x); std::ssize(x); x[arg]; std::begin(x) + 2; })
{
Bounds.expects(0 <= arg && arg < std::ssize(x), "out of bounds access attempt detected" CPP2_SOURCE_LOCATION_ARG);
Bounds.expects(0 <= arg && arg < [&]() -> auto {
if constexpr (std::is_signed_v<CPP2_TYPEOF(arg)>) { return std::ssize(x); }
else { return std::size(x); }
}(), "out of bounds access attempt detected" CPP2_SOURCE_LOCATION_ARG);
return CPP2_FORWARD(x) [ CPP2_FORWARD(arg) ];
}

auto assert_in_bounds(auto&& x, auto&& arg CPP2_SOURCE_LOCATION_PARAM_WITH_DEFAULT) -> decltype(auto)
requires (!(std::is_integral_v<CPP2_TYPEOF(arg)> &&
requires { std::ssize(x); x[arg]; std::begin(x) + 2; }))
{
return CPP2_FORWARD(x) [ CPP2_FORWARD(arg) ];
}
Expand Down