Skip to content

Conversation

maflcko
Copy link
Member

@maflcko maflcko commented Sep 13, 2022

For optional constructor arguments, use a new struct. This comes with two benefits:

  • Earlier unused optional arguments can be omitted
  • Designated initializers can be used

Copy link
Contributor

@stickies-v stickies-v left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ACK fa53da8

Couldn't find any behaviour change. Makes the code more readable at reasonable increased verbosity.

const std::string oneline_description = "",
const std::vector<std::string> type_str = {},
const bool hidden = false)
RPCArgOptions opts = {})
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Since these are always (I think?) rvalue refs, could avoid copying? + for the other ctor

Suggested change
RPCArgOptions opts = {})
RPCArgOptions&& opts = {})

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't think adding && here will avoid a copy. && is just a reference type that forces the caller to pass in an rvalue(ref). This would only avoid a copy if it was an lvalue, cast to an rvalue. Though, in that case we might want to re-use the lvalue and not force the caller to move it (or explicitly copy it), so I think RPCArgOptions is fine here.

While both are fine here, a pure r-value and an r-value ref, I think it is fine to only use && in cases where a copy can be avoided and the copy would be costly (e.g. a8f6954)

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think you're right that there is no need for && here, but I think the reason is that since c++17 mandatory copy elision avoids copying the temporary in the first place?

Without copy elision, I think passing by value instead of rvalue ref would have required a constructing and then copy-constructing operation, as pass by value always means copying (except with elision)?

Drifting off-topic, so you can consider this resolved. Don't want to waste anyone's time.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I guess you are right that for arguments copy elision is not mandatory, but I'd be surprised to see a compiler that wouldn't do it voluntarily.

Though, happy to change to &&. If other reviewers see this they can use 👎 and 👍

Copy link
Contributor

@stickies-v stickies-v left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

re-ACK fa2c72d

@DrahtBot
Copy link
Contributor

DrahtBot commented Sep 14, 2022

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

Conflicts

Reviewers, this pull request conflicts with the following ones:

  • #25093 (rpc: Check for omitted, but required parameters by MarcoFalke)
  • #23549 (Add scanblocks RPC call (attempt 2) by jamesob)

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.

@maflcko maflcko merged commit 33eef56 into bitcoin:master Sep 30, 2022
@maflcko maflcko deleted the 2209-rpc-refactor-🐿 branch September 30, 2022 08:09
sidhujag pushed a commit to syscoin/syscoin that referenced this pull request Oct 4, 2022
…tializers

fa2c72d rpc: Set RPCArg options with designated initializers (MacroFake)

Pull request description:

  For optional constructor arguments, use a new struct. This comes with two benefits:
  * Earlier unused optional arguments can be omitted
  * Designated initializers can be used

ACKs for top commit:
  stickies-v:
    re-ACK fa2c72d

Tree-SHA512: 2a0619548187cc7437fee2466ac4780746490622f202659f53641be01bc2a1fea4416d1a77f3e963bf7c4cce62899b61fab0b9683440cf82f68be44f63826658
@bitcoin bitcoin locked and limited conversation to collaborators Sep 30, 2023
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants