The devil is in the details here, I think. :) const CRPCCommand command
creates copy of the original command, and the caller can maintain the original for the whole execution lifetime, but it would still break (try: the updated gist).
It’s a slightly different story with RPCAddConversion(const std::string& method, int idx)
, because the lifetime the method name seems to be extended, where Temporary object lifetime, note 1 appears to apply to my understanding.
Upfront: using const CRPCCommand& command
works, but the caller must still take care of the lifetime, which is not very intuitive imho, as one might be tempted to believe it’s behavior is similar to the const std::string& s
case, thus my comment about passing a pointer const CRPCCommand*
instead, which makes it very clear that it’s the caller’s responsibility (in code: https://github.com/dexX7/bitcoin/commit/57b252e4c7665542dd6ff92b7e9aafab77d8811a).
Messing around with object lifetime and pointers is a bit cumbersome and error prone in general in my opinion, so it might be worth to overthink the whole approach of storing pointers in mapCommands
, but on the other hand, keeping changes minimal and doing it step by step probably yields results more quickly.