Skip to content

Commit 39ddd52

Browse files
author
MacroFake
committed
Merge bitcoin/bitcoin#24531: Use designated initializers
fa72e0b Use designated initializers (MarcoFalke) Pull request description: Designated initializers are supported since gcc 4.7 (Our minimum required is 8) and clang 3 (Our minimum required is 7). They work out of the box with C++17, and only msvc requires the C++20 flag to be set. I don't expect any of our msvc users will run into issues due to this. See also https://bitcoin.jonasschnelli.ch/ircmeetings/logs/bitcoin-core-dev/2022/bitcoin-core-dev.2022-03-10-19.00.log.html#l-114 ACKs for top commit: kristapsk: ACK fa72e0b hebasto: ACK fa72e0b Tree-SHA512: a198e9addd9af69262a7e79ae4377b55697c8dfe768b8b3d444526544b1d1f85df46f0ae81b7541bf2f73e5868fb944b159e5bf234303c7b8b9d778afb0b2840
2 parents 1c7ef0a + fa72e0b commit 39ddd52

File tree

4 files changed

+38
-7
lines changed

4 files changed

+38
-7
lines changed

build_msvc/common.init.vcxproj.in

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -90,7 +90,7 @@
9090
<AdditionalOptions>/utf-8 /Zc:__cplusplus /std:c++17 %(AdditionalOptions)</AdditionalOptions>
9191
<DisableSpecificWarnings>4018;4244;4267;4334;4715;4805;4834</DisableSpecificWarnings>
9292
<TreatWarningAsError>true</TreatWarningAsError>
93-
<PreprocessorDefinitions>_SILENCE_CXX17_CODECVT_HEADER_DEPRECATION_WARNING;_SILENCE_CXX17_OLD_ALLOCATOR_MEMBERS_DEPRECATION_WARNING;ZMQ_STATIC;NOMINMAX;WIN32;HAVE_CONFIG_H;_CRT_SECURE_NO_WARNINGS;_SCL_SECURE_NO_WARNINGS;_CONSOLE;_WIN32_WINNT=0x0601;_WIN32_IE=0x0501;WIN32_LEAN_AND_MEAN;%(PreprocessorDefinitions)</PreprocessorDefinitions>
93+
<PreprocessorDefinitions>DISABLE_DESIGNATED_INITIALIZER_ERRORS;_SILENCE_CXX17_CODECVT_HEADER_DEPRECATION_WARNING;_SILENCE_CXX17_OLD_ALLOCATOR_MEMBERS_DEPRECATION_WARNING;ZMQ_STATIC;NOMINMAX;WIN32;HAVE_CONFIG_H;_CRT_SECURE_NO_WARNINGS;_SCL_SECURE_NO_WARNINGS;_CONSOLE;_WIN32_WINNT=0x0601;_WIN32_IE=0x0501;WIN32_LEAN_AND_MEAN;%(PreprocessorDefinitions)</PreprocessorDefinitions>
9494
<AdditionalIncludeDirectories>..\..\src;..\..\src\minisketch\include;..\..\src\univalue\include;..\..\src\secp256k1\include;..\..\src\leveldb\include;..\..\src\leveldb\helpers\memenv;%(AdditionalIncludeDirectories)</AdditionalIncludeDirectories>
9595
</ClCompile>
9696
<Link>

src/Makefile.am

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -254,6 +254,7 @@ BITCOIN_CORE_H = \
254254
util/bip32.h \
255255
util/bytevectorhash.h \
256256
util/check.h \
257+
util/designator.h \
257258
util/epochguard.h \
258259
util/error.h \
259260
util/fastrange.h \

src/net.cpp

Lines changed: 15 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@
2525
#include <protocol.h>
2626
#include <random.h>
2727
#include <scheduler.h>
28+
#include <util/designator.h>
2829
#include <util/sock.h>
2930
#include <util/strencodings.h>
3031
#include <util/syscall_sandbox.h>
@@ -1101,12 +1102,20 @@ bool CConnman::AttemptToEvictConnection()
11011102
continue;
11021103
if (node->fDisconnect)
11031104
continue;
1104-
NodeEvictionCandidate candidate = {node->GetId(), node->m_connected, node->m_min_ping_time,
1105-
node->m_last_block_time, node->m_last_tx_time,
1106-
HasAllDesirableServiceFlags(node->nServices),
1107-
node->m_relays_txs.load(), node->m_bloom_filter_loaded.load(),
1108-
node->nKeyedNetGroup, node->m_prefer_evict, node->addr.IsLocal(),
1109-
node->ConnectedThroughNetwork()};
1105+
NodeEvictionCandidate candidate{
1106+
Desig(id) node->GetId(),
1107+
Desig(m_connected) node->m_connected,
1108+
Desig(m_min_ping_time) node->m_min_ping_time,
1109+
Desig(m_last_block_time) node->m_last_block_time,
1110+
Desig(m_last_tx_time) node->m_last_tx_time,
1111+
Desig(fRelevantServices) HasAllDesirableServiceFlags(node->nServices),
1112+
Desig(m_relay_txs) node->m_relays_txs.load(),
1113+
Desig(fBloomFilter) node->m_bloom_filter_loaded.load(),
1114+
Desig(nKeyedNetGroup) node->nKeyedNetGroup,
1115+
Desig(prefer_evict) node->m_prefer_evict,
1116+
Desig(m_is_local) node->addr.IsLocal(),
1117+
Desig(m_network) node->ConnectedThroughNetwork(),
1118+
};
11101119
vEvictionCandidates.push_back(candidate);
11111120
}
11121121
}

src/util/designator.h

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
// Copyright (c) 2022 The Bitcoin Core developers
2+
// Distributed under the MIT software license, see the accompanying
3+
// file COPYING or http://www.opensource.org/licenses/mit-license.php.
4+
5+
#ifndef BITCOIN_UTIL_DESIGNATOR_H
6+
#define BITCOIN_UTIL_DESIGNATOR_H
7+
8+
/**
9+
* Designated initializers can be used to avoid ordering mishaps in aggregate
10+
* initialization. However, they do not prevent uninitialized members. The
11+
* checks can be disabled by defining DISABLE_DESIGNATED_INITIALIZER_ERRORS.
12+
* This should only be needed on MSVC 2019. MSVC 2022 supports them with the
13+
* option "/std:c++20"
14+
*/
15+
#ifndef DISABLE_DESIGNATED_INITIALIZER_ERRORS
16+
#define Desig(field_name) .field_name =
17+
#else
18+
#define Desig(field_name)
19+
#endif
20+
21+
#endif // BITCOIN_UTIL_DESIGNATOR_H

0 commit comments

Comments
 (0)