Skip to content

Commit da78bfe

Browse files
authored
GH-46529: [C++] Convert static inline type trait functions to constexpr (#46559)
### Rationale for this change As C++ versions have increased certain patterns that used to be optimal have been replaced with new more powerful forms. In this scenario static inline functions can safely be replaced with constexpr. ### What changes are included in this PR? Changing type trait related functions that are static inline to instead be constexpr. ### Are these changes tested? Yes * GitHub Issue: #46529 Authored-by: David Sherrier <davidsherrier1997@gmail.com> Signed-off-by: Antoine Pitrou <antoine@python.org>
1 parent 1cabcb3 commit da78bfe

5 files changed

Lines changed: 42 additions & 48 deletions

File tree

cpp/src/arrow/tensor.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@
3333

3434
namespace arrow {
3535

36-
static inline bool is_tensor_supported(Type::type type_id) {
36+
constexpr bool is_tensor_supported(Type::type type_id) {
3737
switch (type_id) {
3838
case Type::UINT8:
3939
case Type::INT8:

cpp/src/arrow/type.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -178,7 +178,7 @@ class ARROW_EXPORT DataType : public std::enable_shared_from_this<DataType>,
178178
virtual DataTypeLayout layout() const = 0;
179179

180180
/// \brief Return the type category
181-
Type::type id() const { return id_; }
181+
constexpr Type::type id() const { return id_; }
182182

183183
/// \brief Return the type category of the storage type
184184
virtual Type::type storage_id() const { return id_; }

cpp/src/arrow/type_traits.h

Lines changed: 26 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -1523,7 +1523,7 @@ constexpr bool is_union(Type::type type_id) {
15231523
///
15241524
/// For Type::FIXED_SIZE_BINARY, you will instead need to inspect the concrete
15251525
/// DataType to get this information.
1526-
static inline int bit_width(Type::type type_id) {
1526+
constexpr int bit_width(Type::type type_id) {
15271527
switch (type_id) {
15281528
case Type::BOOL:
15291529
return 1;
@@ -1579,7 +1579,7 @@ static inline int bit_width(Type::type type_id) {
15791579
///
15801580
/// \param[in] type_id the type-id to check
15811581
/// \return the offsets bit width, or 0 if the type does not have offsets
1582-
static inline int offset_bit_width(Type::type type_id) {
1582+
constexpr int offset_bit_width(Type::type type_id) {
15831583
switch (type_id) {
15841584
case Type::STRING:
15851585
case Type::BINARY:
@@ -1628,15 +1628,15 @@ int RequiredValueAlignmentForBuffer(Type::type type_id, int buffer_index);
16281628
/// \return whether type is an integer type
16291629
///
16301630
/// Convenience for checking using the type's id
1631-
static inline bool is_integer(const DataType& type) { return is_integer(type.id()); }
1631+
constexpr bool is_integer(const DataType& type) { return is_integer(type.id()); }
16321632

16331633
/// \brief Check for a signed integer type
16341634
///
16351635
/// \param[in] type the type to check
16361636
/// \return whether type is a signed integer type
16371637
///
16381638
/// Convenience for checking using the type's id
1639-
static inline bool is_signed_integer(const DataType& type) {
1639+
constexpr bool is_signed_integer(const DataType& type) {
16401640
return is_signed_integer(type.id());
16411641
}
16421642

@@ -1646,7 +1646,7 @@ static inline bool is_signed_integer(const DataType& type) {
16461646
/// \return whether type is an unsigned integer type
16471647
///
16481648
/// Convenience for checking using the type's id
1649-
static inline bool is_unsigned_integer(const DataType& type) {
1649+
constexpr bool is_unsigned_integer(const DataType& type) {
16501650
return is_unsigned_integer(type.id());
16511651
}
16521652

@@ -1656,39 +1656,39 @@ static inline bool is_unsigned_integer(const DataType& type) {
16561656
/// \return whether type is a floating point type
16571657
///
16581658
/// Convenience for checking using the type's id
1659-
static inline bool is_floating(const DataType& type) { return is_floating(type.id()); }
1659+
constexpr bool is_floating(const DataType& type) { return is_floating(type.id()); }
16601660

16611661
/// \brief Check for a numeric type (number except boolean type)
16621662
///
16631663
/// \param[in] type the type to check
16641664
/// \return whether type is a numeric type
16651665
///
16661666
/// Convenience for checking using the type's id
1667-
static inline bool is_numeric(const DataType& type) { return is_numeric(type.id()); }
1667+
constexpr bool is_numeric(const DataType& type) { return is_numeric(type.id()); }
16681668

16691669
/// \brief Check for a decimal type
16701670
///
16711671
/// \param[in] type the type to check
16721672
/// \return whether type is a decimal type
16731673
///
16741674
/// Convenience for checking using the type's id
1675-
static inline bool is_decimal(const DataType& type) { return is_decimal(type.id()); }
1675+
constexpr bool is_decimal(const DataType& type) { return is_decimal(type.id()); }
16761676

16771677
/// \brief Check for a primitive type
16781678
///
16791679
/// \param[in] type the type to check
16801680
/// \return whether type is a primitive type
16811681
///
16821682
/// Convenience for checking using the type's id
1683-
static inline bool is_primitive(const DataType& type) { return is_primitive(type.id()); }
1683+
constexpr bool is_primitive(const DataType& type) { return is_primitive(type.id()); }
16841684

16851685
/// \brief Check for a binary or string-like type (except fixed-size binary)
16861686
///
16871687
/// \param[in] type the type to check
16881688
/// \return whether type is a binary or string-like type
16891689
///
16901690
/// Convenience for checking using the type's id
1691-
static inline bool is_base_binary_like(const DataType& type) {
1691+
constexpr bool is_base_binary_like(const DataType& type) {
16921692
return is_base_binary_like(type.id());
16931693
}
16941694

@@ -1698,17 +1698,15 @@ static inline bool is_base_binary_like(const DataType& type) {
16981698
/// \return whether type is a binary-like type
16991699
///
17001700
/// Convenience for checking using the type's id
1701-
static inline bool is_binary_like(const DataType& type) {
1702-
return is_binary_like(type.id());
1703-
}
1701+
constexpr bool is_binary_like(const DataType& type) { return is_binary_like(type.id()); }
17041702

17051703
/// \brief Check for a large-binary-like type
17061704
///
17071705
/// \param[in] type the type to check
17081706
/// \return whether type is a large-binary-like type
17091707
///
17101708
/// Convenience for checking using the type's id
1711-
static inline bool is_large_binary_like(const DataType& type) {
1709+
constexpr bool is_large_binary_like(const DataType& type) {
17121710
return is_large_binary_like(type.id());
17131711
}
17141712

@@ -1718,23 +1716,23 @@ static inline bool is_large_binary_like(const DataType& type) {
17181716
/// \return whether type is a binary type
17191717
///
17201718
/// Convenience for checking using the type's id
1721-
static inline bool is_binary(const DataType& type) { return is_binary(type.id()); }
1719+
constexpr bool is_binary(const DataType& type) { return is_binary(type.id()); }
17221720

17231721
/// \brief Check for a string type
17241722
///
17251723
/// \param[in] type the type to check
17261724
/// \return whether type is a string type
17271725
///
17281726
/// Convenience for checking using the type's id
1729-
static inline bool is_string(const DataType& type) { return is_string(type.id()); }
1727+
constexpr bool is_string(const DataType& type) { return is_string(type.id()); }
17301728

17311729
/// \brief Check for a binary-view-like type
17321730
///
17331731
/// \param[in] type the type to check
17341732
/// \return whether type is a binary-view-like type
17351733
///
17361734
/// Convenience for checking using the type's id
1737-
static inline bool is_binary_view_like(const DataType& type) {
1735+
constexpr bool is_binary_view_like(const DataType& type) {
17381736
return is_binary_view_like(type.id());
17391737
}
17401738

@@ -1744,33 +1742,31 @@ static inline bool is_binary_view_like(const DataType& type) {
17441742
/// \return whether type is a temporal type
17451743
///
17461744
/// Convenience for checking using the type's id
1747-
static inline bool is_temporal(const DataType& type) { return is_temporal(type.id()); }
1745+
constexpr bool is_temporal(const DataType& type) { return is_temporal(type.id()); }
17481746

17491747
/// \brief Check for an interval type
17501748
///
17511749
/// \param[in] type the type to check
17521750
/// \return whether type is a interval type
17531751
///
17541752
/// Convenience for checking using the type's id
1755-
static inline bool is_interval(const DataType& type) { return is_interval(type.id()); }
1753+
constexpr bool is_interval(const DataType& type) { return is_interval(type.id()); }
17561754

17571755
/// \brief Check for a dictionary type
17581756
///
17591757
/// \param[in] type the type to check
17601758
/// \return whether type is a dictionary type
17611759
///
17621760
/// Convenience for checking using the type's id
1763-
static inline bool is_dictionary(const DataType& type) {
1764-
return is_dictionary(type.id());
1765-
}
1761+
constexpr bool is_dictionary(const DataType& type) { return is_dictionary(type.id()); }
17661762

17671763
/// \brief Check for a fixed-size-binary type
17681764
///
17691765
/// \param[in] type the type to check
17701766
/// \return whether type is a fixed-size-binary type
17711767
///
17721768
/// Convenience for checking using the type's id
1773-
static inline bool is_fixed_size_binary(const DataType& type) {
1769+
constexpr bool is_fixed_size_binary(const DataType& type) {
17741770
return is_fixed_size_binary(type.id());
17751771
}
17761772

@@ -1780,17 +1776,15 @@ static inline bool is_fixed_size_binary(const DataType& type) {
17801776
/// \return whether type is a fixed-width type
17811777
///
17821778
/// Convenience for checking using the type's id
1783-
static inline bool is_fixed_width(const DataType& type) {
1784-
return is_fixed_width(type.id());
1785-
}
1779+
constexpr bool is_fixed_width(const DataType& type) { return is_fixed_width(type.id()); }
17861780

17871781
/// \brief Check for a variable-length list type
17881782
///
17891783
/// \param[in] type the type to check
17901784
/// \return whether type is a variable-length list type
17911785
///
17921786
/// Convenience for checking using the type's id
1793-
static inline bool is_var_length_list(const DataType& type) {
1787+
constexpr bool is_var_length_list(const DataType& type) {
17941788
return is_var_length_list(type.id());
17951789
}
17961790

@@ -1800,15 +1794,15 @@ static inline bool is_var_length_list(const DataType& type) {
18001794
/// \return whether type is a list-like type
18011795
///
18021796
/// Convenience for checking using the type's id
1803-
static inline bool is_list_like(const DataType& type) { return is_list_like(type.id()); }
1797+
constexpr bool is_list_like(const DataType& type) { return is_list_like(type.id()); }
18041798

18051799
/// \brief Check for a var-length list or list-view like type
18061800
///
18071801
/// \param[in] type the type to check
18081802
/// \return whether type is a var-length list or list-view like type
18091803
///
18101804
/// Convenience for checking using the type's id
1811-
static inline bool is_var_length_list_like(const DataType& type) {
1805+
constexpr bool is_var_length_list_like(const DataType& type) {
18121806
return is_var_length_list_like(type.id());
18131807
}
18141808

@@ -1818,23 +1812,23 @@ static inline bool is_var_length_list_like(const DataType& type) {
18181812
/// \return whether type is a list-view type
18191813
///
18201814
/// Convenience for checking using the type's id
1821-
static inline bool is_list_view(const DataType& type) { return is_list_view(type.id()); }
1815+
constexpr bool is_list_view(const DataType& type) { return is_list_view(type.id()); }
18221816

18231817
/// \brief Check for a nested type
18241818
///
18251819
/// \param[in] type the type to check
18261820
/// \return whether type is a nested type
18271821
///
18281822
/// Convenience for checking using the type's id
1829-
static inline bool is_nested(const DataType& type) { return is_nested(type.id()); }
1823+
constexpr bool is_nested(const DataType& type) { return is_nested(type.id()); }
18301824

18311825
/// \brief Check for a union type
18321826
///
18331827
/// \param[in] type the type to check
18341828
/// \return whether type is a union type
18351829
///
18361830
/// Convenience for checking using the type's id
1837-
static inline bool is_union(const DataType& type) { return is_union(type.id()); }
1831+
constexpr bool is_union(const DataType& type) { return is_union(type.id()); }
18381832

18391833
/// @}
18401834

cpp/src/gandiva/arrow.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,7 @@ static inline bool is_decimal_128(DataTypePtr type) {
5555
}
5656
}
5757

58-
static inline bool IsArrowStringLiteral(arrow::Type::type type) {
58+
constexpr bool IsArrowStringLiteral(arrow::Type::type type) {
5959
return type == arrow::Type::STRING || type == arrow::Type::BINARY;
6060
}
6161

cpp/src/parquet/thrift_internal.h

Lines changed: 13 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -57,28 +57,28 @@ namespace parquet {
5757

5858
// Unsafe enum converters (input is not checked for validity)
5959

60-
static inline Type::type FromThriftUnsafe(format::Type::type type) {
60+
constexpr Type::type FromThriftUnsafe(format::Type::type type) {
6161
return static_cast<Type::type>(type);
6262
}
6363

64-
static inline ConvertedType::type FromThriftUnsafe(format::ConvertedType::type type) {
64+
constexpr ConvertedType::type FromThriftUnsafe(format::ConvertedType::type type) {
6565
// item 0 is NONE
6666
return static_cast<ConvertedType::type>(static_cast<int>(type) + 1);
6767
}
6868

69-
static inline Repetition::type FromThriftUnsafe(format::FieldRepetitionType::type type) {
69+
constexpr Repetition::type FromThriftUnsafe(format::FieldRepetitionType::type type) {
7070
return static_cast<Repetition::type>(type);
7171
}
7272

7373
static inline Encoding::type FromThriftUnsafe(format::Encoding::type type) {
7474
return static_cast<Encoding::type>(type);
7575
}
7676

77-
static inline PageType::type FromThriftUnsafe(format::PageType::type type) {
77+
constexpr PageType::type FromThriftUnsafe(format::PageType::type type) {
7878
return static_cast<PageType::type>(type);
7979
}
8080

81-
static inline Compression::type FromThriftUnsafe(format::CompressionCodec::type type) {
81+
constexpr Compression::type FromThriftUnsafe(format::CompressionCodec::type type) {
8282
switch (type) {
8383
case format::CompressionCodec::UNCOMPRESSED:
8484
return Compression::UNCOMPRESSED;
@@ -102,11 +102,11 @@ static inline Compression::type FromThriftUnsafe(format::CompressionCodec::type
102102
}
103103
}
104104

105-
static inline BoundaryOrder::type FromThriftUnsafe(format::BoundaryOrder::type type) {
105+
constexpr BoundaryOrder::type FromThriftUnsafe(format::BoundaryOrder::type type) {
106106
return static_cast<BoundaryOrder::type>(type);
107107
}
108108

109-
static inline GeometryLogicalType::EdgeInterpolationAlgorithm FromThriftUnsafe(
109+
constexpr GeometryLogicalType::EdgeInterpolationAlgorithm FromThriftUnsafe(
110110
format::EdgeInterpolationAlgorithm::type type) {
111111
switch (type) {
112112
case format::EdgeInterpolationAlgorithm::SPHERICAL:
@@ -376,11 +376,11 @@ static inline SizeStatistics FromThrift(const format::SizeStatistics& size_stats
376376
// ----------------------------------------------------------------------
377377
// Convert Thrift enums from Parquet enums
378378

379-
static inline format::Type::type ToThrift(Type::type type) {
379+
constexpr format::Type::type ToThrift(Type::type type) {
380380
return static_cast<format::Type::type>(type);
381381
}
382382

383-
static inline format::ConvertedType::type ToThrift(ConvertedType::type type) {
383+
constexpr format::ConvertedType::type ToThrift(ConvertedType::type type) {
384384
// item 0 is NONE
385385
ARROW_DCHECK_NE(type, ConvertedType::NONE);
386386
// it is forbidden to emit "NA" (PARQUET-1990)
@@ -389,15 +389,15 @@ static inline format::ConvertedType::type ToThrift(ConvertedType::type type) {
389389
return static_cast<format::ConvertedType::type>(static_cast<int>(type) - 1);
390390
}
391391

392-
static inline format::FieldRepetitionType::type ToThrift(Repetition::type type) {
392+
constexpr format::FieldRepetitionType::type ToThrift(Repetition::type type) {
393393
return static_cast<format::FieldRepetitionType::type>(type);
394394
}
395395

396-
static inline format::Encoding::type ToThrift(Encoding::type type) {
396+
constexpr format::Encoding::type ToThrift(Encoding::type type) {
397397
return static_cast<format::Encoding::type>(type);
398398
}
399399

400-
static inline format::CompressionCodec::type ToThrift(Compression::type type) {
400+
constexpr format::CompressionCodec::type ToThrift(Compression::type type) {
401401
switch (type) {
402402
case Compression::UNCOMPRESSED:
403403
return format::CompressionCodec::UNCOMPRESSED;
@@ -422,7 +422,7 @@ static inline format::CompressionCodec::type ToThrift(Compression::type type) {
422422
}
423423
}
424424

425-
static inline format::BoundaryOrder::type ToThrift(BoundaryOrder::type type) {
425+
constexpr format::BoundaryOrder::type ToThrift(BoundaryOrder::type type) {
426426
switch (type) {
427427
case BoundaryOrder::Unordered:
428428
case BoundaryOrder::Ascending:

0 commit comments

Comments
 (0)