Skip to content

Commit 74bcdf0

Browse files
committed
merge bitcoin#29484: replace char-is-int8_t autoconf detection with c++20 concept
1 parent e317a5c commit 74bcdf0

File tree

2 files changed

+10
-14
lines changed

2 files changed

+10
-14
lines changed

configure.ac

Lines changed: 0 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1215,14 +1215,6 @@ AC_COMPILE_IFELSE([AC_LANG_PROGRAM([[#include <sys/types.h>
12151215
[ AC_MSG_RESULT([no])]
12161216
)
12171217

1218-
AC_MSG_CHECKING([for if type char equals int8_t])
1219-
AC_COMPILE_IFELSE([AC_LANG_PROGRAM([[#include <stdint.h>
1220-
#include <type_traits>]],
1221-
[[ static_assert(std::is_same<int8_t, char>::value, ""); ]])],
1222-
[ AC_MSG_RESULT([yes]); AC_DEFINE([CHAR_EQUALS_INT8], [1], [Define this symbol if type char equals int8_t]) ],
1223-
[ AC_MSG_RESULT([no])]
1224-
)
1225-
12261218
dnl ensure backtrace() is found, check -lexecinfo if necessary
12271219
if test "$TARGET_OS" != "windows"; then
12281220
if test "$enable_stacktraces" != "no"; then

src/serialize.h

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010

1111
#include <algorithm>
1212
#include <atomic>
13+
#include <concepts>
1314
#include <cstdint>
1415
#include <cstring>
1516
#include <ios>
@@ -193,9 +194,14 @@ template<typename X> const X& ReadWriteAsHelper(const X& x) { return x; }
193194
FORMATTER_METHODS(cls, obj)
194195

195196
// clang-format off
196-
#ifndef CHAR_EQUALS_INT8
197-
template <typename Stream> void Serialize(Stream&, char) = delete; // char serialization forbidden. Use uint8_t or int8_t
198-
#endif
197+
198+
// Typically int8_t and char are distinct types, but some systems may define int8_t
199+
// in terms of char. Forbid serialization of char in the typical case, but allow it if
200+
// it's the only way to describe an int8_t.
201+
template<class T>
202+
concept CharNotInt8 = std::same_as<T, char> && !std::same_as<T, int8_t>;
203+
204+
template <typename Stream, CharNotInt8 V> void Serialize(Stream&, V) = delete; // char serialization forbidden. Use uint8_t or int8_t
199205
template <typename Stream> void Serialize(Stream& s, std::byte a) { ser_writedata8(s, uint8_t(a)); }
200206
template<typename Stream> inline void Serialize(Stream& s, int8_t a ) { ser_writedata8(s, a); }
201207
template<typename Stream> inline void Serialize(Stream& s, uint8_t a ) { ser_writedata8(s, a); }
@@ -209,9 +215,7 @@ template<typename Stream, int N> inline void Serialize(Stream& s, const char (&a
209215
template<typename Stream, int N> inline void Serialize(Stream& s, const unsigned char (&a)[N]) { s.write(MakeByteSpan(a)); }
210216
template <typename Stream, typename B> void Serialize(Stream& s, Span<B> span) { (void)/* force byte-type */UCharCast(span.data()); s.write(AsBytes(span)); }
211217

212-
#ifndef CHAR_EQUALS_INT8
213-
template <typename Stream> void Unserialize(Stream&, char) = delete; // char serialization forbidden. Use uint8_t or int8_t
214-
#endif
218+
template <typename Stream, CharNotInt8 V> void Unserialize(Stream&, V) = delete; // char serialization forbidden. Use uint8_t or int8_t
215219
template <typename Stream> void Unserialize(Stream& s, std::byte& a) { a = std::byte{ser_readdata8(s)}; }
216220
template<typename Stream> inline void Unserialize(Stream& s, int8_t& a ) { a = ser_readdata8(s); }
217221
template<typename Stream> inline void Unserialize(Stream& s, uint8_t& a ) { a = ser_readdata8(s); }

0 commit comments

Comments
 (0)