refactor: Use std::bind_front over std::bind #34353

pull maflcko wants to merge 1 commits into bitcoin:master from maflcko:2601-bind-front changing 3 files +23 −23
  1. maflcko commented at 2:48 pm on January 20, 2026: member

    std::bind has many issues:

    • It is verbosely listing all placeholders, but in a meaningless way, because it doesn’t name the args or their types.
    • It silently ignores args passed to it, when one arg is overridden. For example [1] compiles fine on current master.
    • Accidentally duplicated placeholders compile fine as well.
    • Usually the placeholders aren’t even needed.
    • This makes it hard to review, understand, and maintain.

    Fix all issues by using std::bind_front from C++20, which allows to drop the brittle _1, _2, ... placeholders. The replacement should be correct, if the trailing placeholders are ordered.

    Introducing the same silent bug on top of this pull request [2] will now lead to a compile failure.


    [1]

    0diff --git a/src/qt/walletmodel.cpp b/src/qt/walletmodel.cpp
    1index 694fb535b5..7661dd361e 100644
    2--- a/src/qt/walletmodel.cpp
    3+++ b/src/qt/walletmodel.cpp
    4@@ -412,3 +412,3 @@ void WalletModel::subscribeToCoreSignals()
    5     m_handler_status_changed = m_wallet->handleStatusChanged(std::bind(&NotifyKeyStoreStatusChanged, this));
    6-    m_handler_address_book_changed = m_wallet->handleAddressBookChanged(std::bind(NotifyAddressBookChanged, this, std::placeholders::_1, std::placeholders::_2, std::placeholders::_3, std::placeholders::_4, std::placeholders::_5));
    7+    m_handler_address_book_changed = m_wallet->handleAddressBookChanged(std::bind(NotifyAddressBookChanged, this, CTxDestination{}, std::placeholders::_2, std::placeholders::_3, std::placeholders::_4, std::placeholders::_5));
    8     m_handler_transaction_changed = m_wallet->handleTransactionChanged(std::bind(NotifyTransactionChanged, this, std::placeholders::_1, std::placeholders::_2)); 
    

    [2]

    0diff --git a/src/qt/walletmodel.cpp b/src/qt/walletmodel.cpp
    1index 578713c0ab..84cced741c 100644
    2--- a/src/qt/walletmodel.cpp
    3+++ b/src/qt/walletmodel.cpp
    4@@ -412,3 +412,3 @@ void WalletModel::subscribeToCoreSignals()
    5     m_handler_status_changed = m_wallet->handleStatusChanged(std::bind_front(&NotifyKeyStoreStatusChanged, this));
    6-    m_handler_address_book_changed = m_wallet->handleAddressBookChanged(std::bind_front(NotifyAddressBookChanged, this));
    7+    m_handler_address_book_changed = m_wallet->handleAddressBookChanged(std::bind_front(NotifyAddressBookChanged, this, CTxDestination{}));
    8     m_handler_transaction_changed = m_wallet->handleTransactionChanged(std::bind_front(NotifyTransactionChanged, this));
    
  2. refactor: Use std::bind_front over std::bind faa18dceba
  3. DrahtBot added the label Refactoring on Jan 20, 2026
  4. DrahtBot commented at 2:49 pm on January 20, 2026: contributor

    The following sections might be updated with supplementary metadata relevant to reviewers and maintainers.

    Code Coverage & Benchmarks

    For details see: https://corecheck.dev/bitcoin/bitcoin/pulls/34353.

    Reviews

    See the guideline for information on the review process.

    Type Reviewers
    ACK janb84, fjahr, hebasto

    If your review is incorrectly listed, please copy-paste <!–meta-tag:bot-skip–> into the comment that the bot should ignore.

    Conflicts

    Reviewers, this pull request conflicts with the following ones:

    • #34158 (torcontrol: Remove libevent usage by fjahr)
    • #31260 (scripted-diff: Type-safe settings retrieval by ryanofsky)

    If you consider this pull request important, please also help to review the conflicting pull requests. Ideally, start with the one that should be merged first.

    LLM Linter (✨ experimental)

    Possible places where named args for integral literals may be used (e.g. func(x, /*named_arg=*/0) in C++, and func(x, named_arg=0) in Python):

    • std::bind_front(&microTask, std::ref(s), std::ref(mutex), std::ref(counter), -delta + 1, noTime) in src/test/scheduler_tests.cpp

    2026-01-20

  5. hebasto commented at 2:49 pm on January 20, 2026: member
    Concept ACK.
  6. fjahr commented at 4:09 pm on January 20, 2026: contributor
    Concept ACK, just ran into bind issues in a PR recently and then replaced it with bind_front.
  7. janb84 commented at 9:03 am on January 21, 2026: contributor

    cr ACK faa18dceba1dd69703a252f751c233d227164689

    Checked if all (relevant) occurrences where changed, this is the case: src/wallet/wallet.cpp:L1351 not relevant, skipping first argument from signal. remaining occurrences are in qt folder and would need to be changed upstream in the GUI repo.

    This change makes the code less verbose and less error-prone, and possibly have a small performance gain. Good change imho.

  8. DrahtBot requested review from fjahr on Jan 21, 2026
  9. DrahtBot requested review from hebasto on Jan 21, 2026
  10. fjahr commented at 9:32 am on January 21, 2026: contributor
    Code review ACK faa18dceba1dd69703a252f751c233d227164689
  11. maflcko commented at 9:48 am on January 21, 2026: member

    Checked if all (relevant) occurrences where changed, this is the case: src/wallet/wallet.cpp:L1351 not relevant, skipping first argument from signal.

    Correct, the wallet one would ideally use a lambda. However, the remaining GUI ones as well. They are already changed in #34276 to use lambdas, because I needed that anyway. They are excluded here to avoid a code-conflict.

  12. hebasto approved
  13. hebasto commented at 2:03 pm on January 21, 2026: member
    ACK faa18dceba1dd69703a252f751c233d227164689, I have reviewed the code and it looks OK.
  14. fanquake merged this on Jan 21, 2026
  15. fanquake closed this on Jan 21, 2026

  16. maflcko deleted the branch on Jan 21, 2026

github-metadata-mirror

This is a metadata mirror of the GitHub repository bitcoin/bitcoin. This site is not affiliated with GitHub. Content is generated from a GitHub metadata backup.
generated: 2026-01-22 03:13 UTC

This site is hosted by @0xB10C
More mirrored repositories can be found on mirror.b10c.me