HomePhabricator

sync: Use decltype(auto) return type for WITH_LOCK

Description

sync: Use decltype(auto) return type for WITH_LOCK

Summary:
Now that we're using C++17, we can use the decltype(auto) return type
(available since C++14) for functions and lambda expressions.

As demonstrated in this commit, this can simplify cases where previously
the compiler failed to deduce the correct return type.

Just for reference, for the "assign to ref" cases fixed here, there are
3 possible solutions:

  • Return a pointer and immediately deref as used before this commit
  • Make sure the function/lambda returns declspec(auto) as used after this commit
  • Class& i = WITH_LOCK(..., return std::ref(...));

References:

  1. https://en.cppreference.com/w/cpp/language/function#Return_type_deduction
  2. https://en.cppreference.com/w/cpp/language/template_argument_deduction#Other_contexts
  3. https://en.cppreference.com/w/cpp/language/auto
  4. https://en.cppreference.com/w/cpp/language/decltype

Explanations:

  1. https://stackoverflow.com/a/21369192
  2. https://stackoverflow.com/a/21369170
  3. Item 3 in Effective Modern C++ (Scott Meyers) via jnewbery

This is a backport of core#20495

Depends on D14641

Test Plan: ninja all check-all

Reviewers: #bitcoin_abc, Fabien

Reviewed By: #bitcoin_abc, Fabien

Differential Revision: https://reviews.bitcoinabc.org/D14642

Details

Provenance
Carl Dong <contact@carldong.me>Authored on Nov 25 2020, 16:05
PiRKCommitted on Oct 16 2023, 08:44
PiRKPushed on Oct 16 2023, 08:44
Reviewer
Restricted Project
Differential Revision
D14642: sync: Use decltype(auto) return type for WITH_LOCK
Parents
rABC709d378e241a: util/check: stop using lambda for Assert/Assume
Branches
Unknown
Tags
Unknown