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

Allow explicit casts to any type. #91

Merged
merged 1 commit into from
Sep 7, 2023
Merged
Show file tree
Hide file tree
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
2 changes: 1 addition & 1 deletion ml_dtypes/include/int4.h
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ struct i4 {
return std::is_signed<UnderlyingTy>::value ? i4(7) : i4(15);
}

template <typename T, typename = std::enable_if_t<std::is_arithmetic_v<T>>>
template <typename T>
explicit constexpr operator T() const {
return static_cast<T>(v);
}
Expand Down
16 changes: 15 additions & 1 deletion ml_dtypes/tests/int4_test.cc
Original file line number Diff line number Diff line change
Expand Up @@ -287,12 +287,26 @@ TYPED_TEST(Int4Test, ToString) {
}
}

struct CustomInt {
constexpr CustomInt() : x(0) {}
constexpr CustomInt(int x) : x(x) {}
// NOLINTNEXTLINE(google-explicit-constructor)
constexpr operator int() const { return x; }
constexpr bool operator==(const CustomInt& other) const {
return x == other.x;
}

private:
int x;
};

#define GEN_DEST_TYPES(Type) \
std::pair<Type, bool>, std::pair<Type, uint4>, std::pair<Type, uint8_t>, \
std::pair<Type, uint16_t>, std::pair<Type, uint32_t>, \
std::pair<Type, uint64_t>, std::pair<Type, int4>, \
std::pair<Type, int8_t>, std::pair<Type, int16_t>, \
std::pair<Type, int32_t>, std::pair<Type, int64_t>
std::pair<Type, int32_t>, std::pair<Type, int64_t>, \
std::pair<Type, CustomInt>

#define GEN_TYPE_PAIRS() GEN_DEST_TYPES(int4), GEN_DEST_TYPES(uint4)

Expand Down
Loading