Skip to content

Commit a799415

Browse files
committed
Merge bitcoin/bitcoin#31904: refactor: modernize outdated trait patterns using helper aliases (C++14/C++17)
4cd95a2 refactor: modernize remaining outdated trait patterns (Lőrinc) ab2b67f scripted-diff: modernize outdated trait patterns - values (Lőrinc) 8327889 scripted-diff: modernize outdated trait patterns - types (Lőrinc) Pull request description: The use of [`std::underlying_type_t<T>`](https://en.cppreference.com/w/cpp/types/underlying_type) or [`std::is_enum_v<T>`](https://en.cppreference.com/w/cpp/types/is_enum) (and similar ones, introduced in C++14) replace the `typename std::underlying_type<T>::type` and `std::is_enum<T>::value` constructs (available in C++11). The `_t` and `_v` helper alias templates offer a more concise way to extract the type and value directly. I've modified the instances I found in the codebase one-by-one (noticed them while investigating bitcoin/bitcoin#31868), and afterwards extracted scripted diff commits to do the trivial ones automatically. The last commit contains the values that were easier done manually. I've excluded changes from `src/bench/nanobench.h`, `src/leveldb`, `src/minisketch`, `src/span.h` and `src/sync.h` - let me know if you think they should be included instead. A few of the code changes can also be reproduced by clang-tidy (but not all of them): ```bash cmake -B build -DCMAKE_C_COMPILER=clang -DCMAKE_CXX_COMPILER=clang++ -DCMAKE_EXPORT_COMPILE_COMMANDS=ON -DBUILD_BENCH=ON -DBUILD_FUZZ_BINARY=ON -DBUILD_FOR_FUZZING=ON && cmake --build build -j$(nproc) run-clang-tidy -quiet -p build -j $(nproc) -checks='-*,modernize-type-traits' -fix $(git grep -lE '::(value|type)' ./src ':(exclude)src/bench/nanobench.h' ':(exclude)src/leveldb' ':(exclude)src/minisketch' ':(exclude)src/span.h' ':(exclude)src/sync.h') ``` ACKs for top commit: laanwj: Concept and code review ACK 4cd95a2 Tree-SHA512: a4bcf0f267c0f4e02983b4d548ed6f58d464ec379ac5cd1f998b9ec0cf698b53a9f2557a05a342b661f1d94adefc9a0ce2dc8f764d49453aaea95451e2c4c581
2 parents 5f4422d + 4cd95a2 commit a799415

File tree

21 files changed

+47
-47
lines changed

21 files changed

+47
-47
lines changed

src/bench/prevector.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,11 +16,11 @@ struct nontrivial_t {
1616
nontrivial_t() = default;
1717
SERIALIZE_METHODS(nontrivial_t, obj) { READWRITE(obj.x); }
1818
};
19-
static_assert(!std::is_trivially_default_constructible<nontrivial_t>::value,
19+
static_assert(!std::is_trivially_default_constructible_v<nontrivial_t>,
2020
"expected nontrivial_t to not be trivially constructible");
2121

2222
typedef unsigned char trivial_t;
23-
static_assert(std::is_trivially_default_constructible<trivial_t>::value,
23+
static_assert(std::is_trivially_default_constructible_v<trivial_t>,
2424
"expected trivial_t to be trivially constructible");
2525

2626
template <typename T>

src/deploymentstatus.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ static_assert(!ValidDeployment(static_cast<Consensus::BuriedDeployment>(Consensu
2424
template<typename T, T x>
2525
static constexpr bool is_minimum()
2626
{
27-
using U = typename std::underlying_type<T>::type;
27+
using U = std::underlying_type_t<T>;
2828
return x == std::numeric_limits<U>::min();
2929
}
3030

src/interfaces/wallet.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@ enum class AddressPurpose;
4545
enum isminetype : unsigned int;
4646
struct CRecipient;
4747
struct WalletContext;
48-
using isminefilter = std::underlying_type<isminetype>::type;
48+
using isminefilter = std::underlying_type_t<isminetype>;
4949
} // namespace wallet
5050

5151
namespace interfaces {

src/logging/timer.h

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -67,11 +67,11 @@ class Timer
6767
}
6868
const auto duration{end_time - *m_start_t};
6969

70-
if constexpr (std::is_same<TimeType, std::chrono::microseconds>::value) {
70+
if constexpr (std::is_same_v<TimeType, std::chrono::microseconds>) {
7171
return strprintf("%s: %s (%iμs)", m_prefix, msg, Ticks<std::chrono::microseconds>(duration));
72-
} else if constexpr (std::is_same<TimeType, std::chrono::milliseconds>::value) {
72+
} else if constexpr (std::is_same_v<TimeType, std::chrono::milliseconds>) {
7373
return strprintf("%s: %s (%.2fms)", m_prefix, msg, Ticks<MillisecondsDouble>(duration));
74-
} else if constexpr (std::is_same<TimeType, std::chrono::seconds>::value) {
74+
} else if constexpr (std::is_same_v<TimeType, std::chrono::seconds>) {
7575
return strprintf("%s: %s (%.2fs)", m_prefix, msg, Ticks<SecondsDouble>(duration));
7676
} else {
7777
static_assert(ALWAYS_FALSE<TimeType>, "Error: unexpected time type");

src/net_permissions.h

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,7 @@ enum class NetPermissionFlags : uint32_t {
4848
};
4949
static inline constexpr NetPermissionFlags operator|(NetPermissionFlags a, NetPermissionFlags b)
5050
{
51-
using t = typename std::underlying_type<NetPermissionFlags>::type;
51+
using t = std::underlying_type_t<NetPermissionFlags>;
5252
return static_cast<NetPermissionFlags>(static_cast<t>(a) | static_cast<t>(b));
5353
}
5454

@@ -59,7 +59,7 @@ class NetPermissions
5959
static std::vector<std::string> ToStrings(NetPermissionFlags flags);
6060
static inline bool HasFlag(NetPermissionFlags flags, NetPermissionFlags f)
6161
{
62-
using t = typename std::underlying_type<NetPermissionFlags>::type;
62+
using t = std::underlying_type_t<NetPermissionFlags>;
6363
return (static_cast<t>(flags) & static_cast<t>(f)) == static_cast<t>(f);
6464
}
6565
static inline void AddFlag(NetPermissionFlags& flags, NetPermissionFlags f)
@@ -74,7 +74,7 @@ class NetPermissions
7474
static inline void ClearFlag(NetPermissionFlags& flags, NetPermissionFlags f)
7575
{
7676
assert(f == NetPermissionFlags::Implicit);
77-
using t = typename std::underlying_type<NetPermissionFlags>::type;
77+
using t = std::underlying_type_t<NetPermissionFlags>;
7878
flags = static_cast<NetPermissionFlags>(static_cast<t>(flags) & ~static_cast<t>(f));
7979
}
8080
};

src/netbase.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -37,12 +37,12 @@ enum class ConnectionDirection {
3737
Both = (In | Out),
3838
};
3939
static inline ConnectionDirection& operator|=(ConnectionDirection& a, ConnectionDirection b) {
40-
using underlying = typename std::underlying_type<ConnectionDirection>::type;
40+
using underlying = std::underlying_type_t<ConnectionDirection>;
4141
a = ConnectionDirection(underlying(a) | underlying(b));
4242
return a;
4343
}
4444
static inline bool operator&(ConnectionDirection a, ConnectionDirection b) {
45-
using underlying = typename std::underlying_type<ConnectionDirection>::type;
45+
using underlying = std::underlying_type_t<ConnectionDirection>;
4646
return (underlying(a) & underlying(b));
4747
}
4848

src/random.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -334,7 +334,7 @@ class RandomMixin
334334

335335
/** Generate a uniform random duration in the range [0..max). Precondition: max.count() > 0 */
336336
template <StdChronoDuration Dur>
337-
Dur randrange(typename std::common_type_t<Dur> range) noexcept
337+
Dur randrange(std::common_type_t<Dur> range) noexcept
338338
// Having the compiler infer the template argument from the function argument
339339
// is dangerous, because the desired return value generally has a different
340340
// type than the function argument. So std::common_type is used to force the

src/randomenv.cpp

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -70,10 +70,10 @@ namespace {
7070
*/
7171
template<typename T>
7272
CSHA512& operator<<(CSHA512& hasher, const T& data) {
73-
static_assert(!std::is_same<typename std::decay<T>::type, char*>::value, "Calling operator<<(CSHA512, char*) is probably not what you want");
74-
static_assert(!std::is_same<typename std::decay<T>::type, unsigned char*>::value, "Calling operator<<(CSHA512, unsigned char*) is probably not what you want");
75-
static_assert(!std::is_same<typename std::decay<T>::type, const char*>::value, "Calling operator<<(CSHA512, const char*) is probably not what you want");
76-
static_assert(!std::is_same<typename std::decay<T>::type, const unsigned char*>::value, "Calling operator<<(CSHA512, const unsigned char*) is probably not what you want");
73+
static_assert(!std::is_same_v<std::decay_t<T>, char*>, "Calling operator<<(CSHA512, char*) is probably not what you want");
74+
static_assert(!std::is_same_v<std::decay_t<T>, unsigned char*>, "Calling operator<<(CSHA512, unsigned char*) is probably not what you want");
75+
static_assert(!std::is_same_v<std::decay_t<T>, const char*>, "Calling operator<<(CSHA512, const char*) is probably not what you want");
76+
static_assert(!std::is_same_v<std::decay_t<T>, const unsigned char*>, "Calling operator<<(CSHA512, const unsigned char*) is probably not what you want");
7777
hasher.Write((const unsigned char*)&data, sizeof(data));
7878
return hasher;
7979
}

src/rpc/util.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,7 @@ const std::string EXAMPLE_ADDRESS[2] = {"bc1q09vm5lfy0j5reeulh4x5752q25uqqvz34hu
4949
std::string GetAllOutputTypes()
5050
{
5151
std::vector<std::string> ret;
52-
using U = std::underlying_type<TxoutType>::type;
52+
using U = std::underlying_type_t<TxoutType>;
5353
for (U i = (U)TxoutType::NONSTANDARD; i <= (U)TxoutType::WITNESS_UNKNOWN; ++i) {
5454
ret.emplace_back(GetTxnOutputType(static_cast<TxoutType>(i)));
5555
}

src/serialize.h

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -154,7 +154,7 @@ const Out& AsBase(const In& x)
154154
}
155155

156156
#define READWRITE(...) (ser_action.SerReadWriteMany(s, __VA_ARGS__))
157-
#define SER_READ(obj, code) ser_action.SerRead(s, obj, [&](Stream& s, typename std::remove_const<Type>::type& obj) { code; })
157+
#define SER_READ(obj, code) ser_action.SerRead(s, obj, [&](Stream& s, std::remove_const_t<Type>& obj) { code; })
158158
#define SER_WRITE(obj, code) ser_action.SerWrite(s, obj, [&](Stream& s, const Type& obj) { code; })
159159

160160
/**
@@ -220,13 +220,13 @@ const Out& AsBase(const In& x)
220220
template <typename Stream> \
221221
void Serialize(Stream& s) const \
222222
{ \
223-
static_assert(std::is_same<const cls&, decltype(*this)>::value, "Serialize type mismatch"); \
223+
static_assert(std::is_same_v<const cls&, decltype(*this)>, "Serialize type mismatch"); \
224224
Ser(s, *this); \
225225
} \
226226
template <typename Stream> \
227227
void Unserialize(Stream& s) \
228228
{ \
229-
static_assert(std::is_same<cls&, decltype(*this)>::value, "Unserialize type mismatch"); \
229+
static_assert(std::is_same_v<cls&, decltype(*this)>, "Unserialize type mismatch"); \
230230
Unser(s, *this); \
231231
}
232232

@@ -408,8 +408,8 @@ template <VarIntMode Mode, typename I>
408408
struct CheckVarIntMode {
409409
constexpr CheckVarIntMode()
410410
{
411-
static_assert(Mode != VarIntMode::DEFAULT || std::is_unsigned<I>::value, "Unsigned type required with mode DEFAULT.");
412-
static_assert(Mode != VarIntMode::NONNEGATIVE_SIGNED || std::is_signed<I>::value, "Signed type required with mode NONNEGATIVE_SIGNED.");
411+
static_assert(Mode != VarIntMode::DEFAULT || std::is_unsigned_v<I>, "Unsigned type required with mode DEFAULT.");
412+
static_assert(Mode != VarIntMode::NONNEGATIVE_SIGNED || std::is_signed_v<I>, "Signed type required with mode NONNEGATIVE_SIGNED.");
413413
}
414414
};
415415

@@ -474,7 +474,7 @@ I ReadVarInt(Stream& is)
474474
template<typename Formatter, typename T>
475475
class Wrapper
476476
{
477-
static_assert(std::is_lvalue_reference<T>::value, "Wrapper needs an lvalue reference type T");
477+
static_assert(std::is_lvalue_reference_v<T>, "Wrapper needs an lvalue reference type T");
478478
protected:
479479
T m_object;
480480
public:
@@ -507,12 +507,12 @@ struct VarIntFormatter
507507
{
508508
template<typename Stream, typename I> void Ser(Stream &s, I v)
509509
{
510-
WriteVarInt<Stream,Mode,typename std::remove_cv<I>::type>(s, v);
510+
WriteVarInt<Stream,Mode, std::remove_cv_t<I>>(s, v);
511511
}
512512

513513
template<typename Stream, typename I> void Unser(Stream& s, I& v)
514514
{
515-
v = ReadVarInt<Stream,Mode,typename std::remove_cv<I>::type>(s);
515+
v = ReadVarInt<Stream,Mode, std::remove_cv_t<I>>(s);
516516
}
517517
};
518518

@@ -545,7 +545,7 @@ struct CustomUintFormatter
545545

546546
template <typename Stream, typename I> void Unser(Stream& s, I& v)
547547
{
548-
using U = typename std::conditional<std::is_enum<I>::value, std::underlying_type<I>, std::common_type<I>>::type::type;
548+
using U = typename std::conditional_t<std::is_enum_v<I>, std::underlying_type<I>, std::common_type<I>>::type;
549549
static_assert(std::numeric_limits<U>::max() >= MAX && std::numeric_limits<U>::min() <= 0, "Assigned type too small");
550550
uint64_t raw = 0;
551551
if (BigEndian) {
@@ -577,7 +577,7 @@ struct CompactSizeFormatter
577577
template<typename Stream, typename I>
578578
void Ser(Stream& s, I v)
579579
{
580-
static_assert(std::is_unsigned<I>::value, "CompactSize only supported for unsigned integers");
580+
static_assert(std::is_unsigned_v<I>, "CompactSize only supported for unsigned integers");
581581
static_assert(std::numeric_limits<I>::max() <= std::numeric_limits<uint64_t>::max(), "CompactSize only supports 64-bit integers and below");
582582

583583
WriteCompactSize<Stream>(s, v);

0 commit comments

Comments
 (0)