Skip to content

Commit 4c02c6d

Browse files
committed
ARROW-13444: [C++] Remove usage of deprecated std::result_of
Also clean up some UBSAN nullptr arithmetic warnings Closes #10814 from bkietz/13444-C20-compatibility-by-upda Authored-by: Benjamin Kietzman <[email protected]> Signed-off-by: Benjamin Kietzman <[email protected]>
1 parent 551c07c commit 4c02c6d

File tree

10 files changed

+30
-26
lines changed

10 files changed

+30
-26
lines changed

cpp/src/arrow/buffer_builder.h

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -45,8 +45,7 @@ class ARROW_EXPORT BufferBuilder {
4545
explicit BufferBuilder(MemoryPool* pool = default_memory_pool())
4646
: pool_(pool),
4747
data_(/*ensure never null to make ubsan happy and avoid check penalties below*/
48-
&util::internal::non_null_filler),
49-
48+
util::MakeNonNull<uint8_t>()),
5049
capacity_(0),
5150
size_(0) {}
5251

cpp/src/arrow/result.h

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -385,7 +385,8 @@ class ARROW_MUST_USE_TYPE Result : public util::EqualityComparable<Result<T>> {
385385
/// Apply a function to the internally stored value to produce a new result or propagate
386386
/// the stored error.
387387
template <typename M>
388-
typename EnsureResult<typename std::result_of<M && (T)>::type>::type Map(M&& m) && {
388+
typename EnsureResult<decltype(std::declval<M&&>()(std::declval<T&&>()))>::type Map(
389+
M&& m) && {
389390
if (!ok()) {
390391
return status();
391392
}
@@ -395,8 +396,8 @@ class ARROW_MUST_USE_TYPE Result : public util::EqualityComparable<Result<T>> {
395396
/// Apply a function to the internally stored value to produce a new result or propagate
396397
/// the stored error.
397398
template <typename M>
398-
typename EnsureResult<typename std::result_of<M && (const T&)>::type>::type Map(
399-
M&& m) const& {
399+
typename EnsureResult<decltype(std::declval<M&&>()(std::declval<const T&>()))>::type
400+
Map(M&& m) const& {
400401
if (!ok()) {
401402
return status();
402403
}

cpp/src/arrow/testing/util.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -123,7 +123,7 @@ struct VisitBuilderImpl {
123123
template <typename T, typename BuilderType = typename TypeTraits<T>::BuilderType,
124124
// need to let SFINAE drop this Visit when it would result in
125125
// [](NullBuilder*){}(double_builder)
126-
typename E = typename std::result_of<Fn(BuilderType*)>::type>
126+
typename = decltype(std::declval<Fn>()(std::declval<BuilderType*>()))>
127127
Status Visit(const T&) {
128128
fn_(internal::checked_cast<BuilderType*>(builder_));
129129
return Status::OK();

cpp/src/arrow/util/bit_block_counter.cc

Lines changed: 6 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -37,19 +37,12 @@ BitBlockCount BitBlockCounter::GetBlockSlow(int64_t block_size) noexcept {
3737
return {run_length, popcount};
3838
}
3939

40-
// Prevent pointer arithmetic on nullptr, which is undefined behavior even if the pointer
41-
// is never dereferenced.
42-
inline const uint8_t* EnsureNotNull(const uint8_t* ptr) {
43-
static const uint8_t byte{};
44-
return ptr == nullptr ? &byte : ptr;
45-
}
46-
4740
OptionalBitBlockCounter::OptionalBitBlockCounter(const uint8_t* validity_bitmap,
4841
int64_t offset, int64_t length)
4942
: has_bitmap_(validity_bitmap != nullptr),
5043
position_(0),
5144
length_(length),
52-
counter_(EnsureNotNull(validity_bitmap), offset, length) {}
45+
counter_(util::MakeNonNull(validity_bitmap), offset, length) {}
5346

5447
OptionalBitBlockCounter::OptionalBitBlockCounter(
5548
const std::shared_ptr<Buffer>& validity_bitmap, int64_t offset, int64_t length)
@@ -64,10 +57,11 @@ OptionalBinaryBitBlockCounter::OptionalBinaryBitBlockCounter(const uint8_t* left
6457
: has_bitmap_(HasBitmapFromBitmaps(left_bitmap != nullptr, right_bitmap != nullptr)),
6558
position_(0),
6659
length_(length),
67-
unary_counter_(EnsureNotNull(left_bitmap != nullptr ? left_bitmap : right_bitmap),
68-
left_bitmap != nullptr ? left_offset : right_offset, length),
69-
binary_counter_(EnsureNotNull(left_bitmap), left_offset,
70-
EnsureNotNull(right_bitmap), right_offset, length) {}
60+
unary_counter_(
61+
util::MakeNonNull(left_bitmap != nullptr ? left_bitmap : right_bitmap),
62+
left_bitmap != nullptr ? left_offset : right_offset, length),
63+
binary_counter_(util::MakeNonNull(left_bitmap), left_offset,
64+
util::MakeNonNull(right_bitmap), right_offset, length) {}
7165

7266
OptionalBinaryBitBlockCounter::OptionalBinaryBitBlockCounter(
7367
const std::shared_ptr<Buffer>& left_bitmap, int64_t left_offset,

cpp/src/arrow/util/bit_run_reader.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -197,7 +197,7 @@ class BaseSetBitRunReader {
197197
/// \param[in] length number of bits to copy
198198
ARROW_NOINLINE
199199
BaseSetBitRunReader(const uint8_t* bitmap, int64_t start_offset, int64_t length)
200-
: bitmap_(bitmap),
200+
: bitmap_(util::MakeNonNull(bitmap)),
201201
length_(length),
202202
remaining_(length_),
203203
current_word_(0),

cpp/src/arrow/util/bitmap_generate.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,7 @@ void GenerateBits(uint8_t* bitmap, int64_t start_offset, int64_t length, Generat
6262
template <class Generator>
6363
void GenerateBitsUnrolled(uint8_t* bitmap, int64_t start_offset, int64_t length,
6464
Generator&& g) {
65-
static_assert(std::is_same<typename std::result_of<Generator && ()>::type, bool>::value,
65+
static_assert(std::is_same<decltype(std::declval<Generator>()()), bool>::value,
6666
"Functor passed to GenerateBitsUnrolled must return bool");
6767

6868
if (length == 0) {

cpp/src/arrow/util/bitmap_reader.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -76,7 +76,7 @@ class BitmapReader {
7676
class BitmapUInt64Reader {
7777
public:
7878
BitmapUInt64Reader(const uint8_t* bitmap, int64_t start_offset, int64_t length)
79-
: bitmap_(bitmap + start_offset / 8),
79+
: bitmap_(util::MakeNonNull(bitmap) + start_offset / 8),
8080
num_carry_bits_(8 - start_offset % 8),
8181
length_(length),
8282
remaining_length_(length_) {

cpp/src/arrow/util/functional.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -129,7 +129,7 @@ class FnOnce<R(A...)> {
129129

130130
template <typename Fn,
131131
typename = typename std::enable_if<std::is_convertible<
132-
typename std::result_of<Fn && (A...)>::type, R>::value>::type>
132+
decltype(std::declval<Fn&&>()(std::declval<A>()...)), R>::value>::type>
133133
FnOnce(Fn fn) : impl_(new FnImpl<Fn>(std::move(fn))) { // NOLINT runtime/explicit
134134
}
135135

cpp/src/arrow/util/future.h

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@
2828
#include "arrow/result.h"
2929
#include "arrow/status.h"
3030
#include "arrow/type_fwd.h"
31+
#include "arrow/type_traits.h"
3132
#include "arrow/util/functional.h"
3233
#include "arrow/util/macros.h"
3334
#include "arrow/util/optional.h"
@@ -47,8 +48,17 @@ struct is_future : std::false_type {};
4748
template <typename T>
4849
struct is_future<Future<T>> : std::true_type {};
4950

51+
template <typename Signature, typename Enable = void>
52+
struct result_of;
53+
54+
template <typename Fn, typename... A>
55+
struct result_of<Fn(A...),
56+
internal::void_t<decltype(std::declval<Fn>()(std::declval<A>()...))>> {
57+
using type = decltype(std::declval<Fn>()(std::declval<A>()...));
58+
};
59+
5060
template <typename Signature>
51-
using result_of_t = typename std::result_of<Signature>::type;
61+
using result_of_t = typename result_of<Signature>::type;
5262

5363
// Helper to find the synchronous counterpart for a Future
5464
template <typename T>

cpp/src/arrow/util/ubsan.h

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ namespace util {
3030

3131
namespace internal {
3232

33-
static uint8_t non_null_filler;
33+
constexpr uint8_t kNonNullFiller = 0;
3434

3535
} // namespace internal
3636

@@ -44,12 +44,12 @@ static uint8_t non_null_filler;
4444
/// https://github.com/google/flatbuffers/pull/5355 is trying to resolve
4545
/// them.
4646
template <typename T>
47-
inline T* MakeNonNull(T* maybe_null) {
47+
inline T* MakeNonNull(T* maybe_null = NULLPTR) {
4848
if (ARROW_PREDICT_TRUE(maybe_null != NULLPTR)) {
4949
return maybe_null;
5050
}
5151

52-
return reinterpret_cast<T*>(&internal::non_null_filler);
52+
return const_cast<T*>(reinterpret_cast<const T*>(&internal::kNonNullFiller));
5353
}
5454

5555
template <typename T>

0 commit comments

Comments
 (0)