Skip to content

Conversation

@mtrofin
Copy link
Member

@mtrofin mtrofin commented Oct 30, 2025

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

Copy link
Member Author

mtrofin commented Oct 30, 2025

This stack of pull requests is managed by Graphite. Learn more about stacking.

@llvmbot
Copy link
Member

llvmbot commented Oct 30, 2025

@llvm/pr-subscribers-llvm-support

@llvm/pr-subscribers-llvm-adt

Author: Mircea Trofin (mtrofin)

Changes

Full diff: https://github.com/llvm/llvm-project/pull/165803.diff

2 Files Affected:

  • (modified) llvm/include/llvm/ADT/STLExtras.h (+6)
  • (modified) llvm/unittests/ADT/STLExtrasTest.cpp (+7)
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);

@github-actions
Copy link

github-actions bot commented Oct 30, 2025

✅ With the latest revision this PR passed the C/C++ code formatter.

@mtrofin mtrofin force-pushed the users/mtrofin/10-30-_adt_introduce_a_static_cast_to_ branch from d07d953 to d4dd4af Compare October 30, 2025 23:48
Copy link
Member

@kuhar kuhar left a 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

@mtrofin mtrofin force-pushed the users/mtrofin/10-30-_adt_introduce_a_static_cast_to_ branch 2 times, most recently from 40b6404 to b0877cd Compare November 3, 2025 18:45
@mtrofin mtrofin changed the title [ADT] Introduce a static_cast_to [ADT] Introduce Casting Predicates Nov 3, 2025
@mtrofin mtrofin force-pushed the users/mtrofin/10-30-_adt_introduce_a_static_cast_to_ branch from b0877cd to 915af89 Compare November 3, 2025 18:47
@mtrofin mtrofin changed the title [ADT] Introduce Casting Predicates [ADT] Introduce Casting function objects Nov 3, 2025
@mtrofin mtrofin force-pushed the users/mtrofin/10-30-_adt_introduce_a_static_cast_to_ branch 2 times, most recently from e7391f5 to 2dd8a7c Compare November 3, 2025 18:53
Copy link
Member Author

mtrofin commented Nov 3, 2025

Cast function objects similar to IsaPred seem like a more general direction

done, ptal

Copy link
Contributor

@kazutakahirata kazutakahirata left a 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.

Copy link
Member

@kuhar kuhar left a 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.

@mtrofin mtrofin force-pushed the users/mtrofin/10-30-_adt_introduce_a_static_cast_to_ branch from 2dd8a7c to b403ec8 Compare November 3, 2025 22:09
Copy link
Member Author

mtrofin commented Nov 3, 2025

The tests can be simplified by casting individual elements instead of combining with map_range.

True, but I wanted to exercise an example of the targeted use case. WDYT?

@mtrofin mtrofin force-pushed the users/mtrofin/10-30-_adt_introduce_a_static_cast_to_ branch from b403ec8 to 81bc5df Compare November 3, 2025 22:11
@mtrofin mtrofin requested a review from kuhar November 4, 2025 20:59
@kuhar
Copy link
Member

kuhar commented Nov 4, 2025

The tests can be simplified by casting individual elements instead of combining with map_range.

True, but I wanted to exercise an example of the targeted use case. WDYT?

We could have that as an extra test case for documentation etc., but otherwise I think it mainly adds complexity we don't need.

@mtrofin mtrofin force-pushed the users/mtrofin/10-30-_adt_introduce_a_static_cast_to_ branch from 81bc5df to 2d73ce3 Compare November 4, 2025 21:34
Copy link
Member Author

mtrofin commented Nov 4, 2025

OK, removed the map_range stuff.

@mtrofin mtrofin force-pushed the users/mtrofin/10-30-_adt_introduce_a_static_cast_to_ branch from 2d73ce3 to 85a8b3a Compare November 4, 2025 21:36
Copy link
Member

@kuhar kuhar left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks great

@mtrofin mtrofin force-pushed the users/mtrofin/10-30-_adt_introduce_a_static_cast_to_ branch from 85a8b3a to 404ab43 Compare November 4, 2025 21:41
@mtrofin mtrofin merged commit 02f5a1a into main Nov 5, 2025
7 of 9 checks passed
@mtrofin mtrofin deleted the users/mtrofin/10-30-_adt_introduce_a_static_cast_to_ branch November 5, 2025 00:39
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

6 participants