-
Notifications
You must be signed in to change notification settings - Fork 15.1k
[ADT] Introduce Casting function objects #165803
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
[ADT] Introduce Casting function objects #165803
Conversation
|
@llvm/pr-subscribers-llvm-support @llvm/pr-subscribers-llvm-adt Author: Mircea Trofin (mtrofin) ChangesFull diff: https://github.com/llvm/llvm-project/pull/165803.diff 2 Files Affected:
diff --git a/llvm/include/llvm/ADT/STLExtras.h b/llvm/include/llvm/ADT/STLExtras.h
index a9841c6651b72..f65d08cafbdc6 100644
--- a/llvm/include/llvm/ADT/STLExtras.h
+++ b/llvm/include/llvm/ADT/STLExtras.h
@@ -1704,6 +1704,12 @@ auto sum_of(R &&Range, E Init = E{0}) {
return accumulate(std::forward<R>(Range), std::move(Init));
}
+/// Return a range where each value of `R` is static_cast to `T`
+template <typename T, typename R>
+auto static_cast_to(R &&Range) {
+ return map_range(Range, [](const auto &V) { return static_cast<T>(V); });
+}
+
/// Returns the product of all values in `Range` with `Init` initial value.
/// The default initial value is 1.
template <typename R, typename E = detail::ValueOfRange<R>>
diff --git a/llvm/unittests/ADT/STLExtrasTest.cpp b/llvm/unittests/ADT/STLExtrasTest.cpp
index 966b1f01e8a31..f9341dd64c999 100644
--- a/llvm/unittests/ADT/STLExtrasTest.cpp
+++ b/llvm/unittests/ADT/STLExtrasTest.cpp
@@ -1669,6 +1669,13 @@ TEST(STLExtrasTest, SumOf) {
EXPECT_EQ(sum_of(V3), 3.0f);
}
+TEST(STLExtrasTest, StaticCastTo) {
+ std::vector<int32_t> R{1, 2, 3};
+ EXPECT_TRUE(all_of(R, [](auto V) { return sizeof(V) == sizeof(int32_t); }));
+ EXPECT_TRUE(all_of(static_cast_to<int64_t>(R),
+ [](auto V) { return sizeof(V) == sizeof(int64_t); }));
+}
+
TEST(STLExtrasTest, ProductOf) {
EXPECT_EQ(product_of(std::vector<int>()), 1);
EXPECT_EQ(product_of(std::vector<int>(), 0), 0);
|
|
✅ With the latest revision this PR passed the C/C++ code formatter. |
d07d953 to
d4dd4af
Compare
kuhar
left a comment
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Cast function objects similar to IsaPred seem like a more general direction
40b6404 to
b0877cd
Compare
static_cast_tob0877cd to
915af89
Compare
e7391f5 to
2dd8a7c
Compare
done, ptal |
kazutakahirata
left a comment
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
LGTM. Let's wait to hear from @kuhar for a day or so.
kuhar
left a comment
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The tests can be simplified by casting individual elements instead of combining with map_range.
2dd8a7c to
b403ec8
Compare
True, but I wanted to exercise an example of the targeted use case. WDYT? |
b403ec8 to
81bc5df
Compare
We could have that as an extra test case for documentation etc., but otherwise I think it mainly adds complexity we don't need. |
81bc5df to
2d73ce3
Compare
|
OK, removed the map_range stuff. |
2d73ce3 to
85a8b3a
Compare
kuhar
left a comment
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Looks great
85a8b3a to
404ab43
Compare

Adding casting function objects as a convenience for expressing e.g.
auto AsDoubles = map_range(RangeOfInts, StaticCastTo<double>)