-
Notifications
You must be signed in to change notification settings - Fork 15k
[ADT] Introduce a static_cast_to
#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
base: main
Are you sure you want to change the base?
[ADT] Introduce a static_cast_to
#165803
Conversation
|
@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
|
|
||
| /// 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); }); |
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.
[](auto &V) -> decltype(auto) { return static_cast<T>(V); }Perhaps -> decltype(auto) is needed to allow returning reference.
| } | ||
|
|
||
| /// Return a range where each value of `R` is static_cast to `T` | ||
| template <typename T, typename R> auto static_cast_to(R &&Range) { |
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.
I think the idea makes sense but I wonder if we could pick a more descriptive function name, similar to other range functions
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.
One alternative would be not to add a range function and instead lift static cast to a function object like we did for llvm::IsaPred<T>. This way we can support variants based on dyn_cast/dyn_cast_if_present/cast.
I think that's a more general solution that composes with other functions like map_to_vector
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.
How would the usage look like?
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.
map_range(Foo, StaticCastTo<unsigned>)
map_range(Bar, DynCastTo<IntegerAttr>)
map_to_vector(Baz, CastTo<FloatType>)
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
d4dd4af to
40b6404
Compare

Adding a
static_cast_to over a range as a convenience.