jeudi 29 juin 2017

Is use count on `const std::shared_ptr<...>' mutable?

I'm trying to adapt some pre-existing code to make use of std::shared_ptr<...>. It is a `message passing system' so the basic context is:

Public Method:

void writeReport(std::shared_ptr<const ReportBase> pReport) {
  /* This is a public method for writing a report. I might do
     additional stuff in here but end by forwarding to the
     private method. */
  writeReport_(pReport);
}

Private Method:

void writeReport_(std::shared_ptr<const ReportBase> pReport) {
  if( certain conditions )
    processReport_(pReport);

  writeBytes_(serialize(pReport));
}

Processing Method:

void processReport_(std::shared_ptr<const ReportBase> pReport) {
  processReportImpl(pReport);

  if( certain other conditions )
    reportDeque_.push_back(pReport);
}

Of the above pseudo-code, for example, processReport_(...) might be the only method which, under certain conditions, would want to actually store the record. The other methods are simply interested in the contents of the object pointed to. So, were it not for the need to sometimes copy the shared_ptr in processReport_(...) (i.e., 'store' the record), I would simply pass const ReportBase * to all my nested functions and avoid the overhead of pass-by-value (i.e., use count increments).

So, I want to pass std::shared_ptr<const ReportBase>& (and maybe && where appropriate) but want to preclude some rogue nested method from actually modifying what the pointer points to. So I think I want to pass const std::shared_ptr<const ReportBase>& to prevent that...

... but, again, in processReport_(...) I'll sometimes want to make a copy to store the record.

Which finally leads me to the question(s)... is use count on a std::shared_ptr mutable? Why can (or can't) I do a copy assignment on a const std::shared_ptr<...> to a std::shared_ptr<...>? Does the const make the entire control block of the shared_ptr const? Or does the leading const only apply to the raw pointer value?

And if someone wants to answer tangentially 'don't worry so much about passing by value into nested functions' or 'I have a completely different approach for you' then I'd be interested to hear that too.

Aucun commentaire:

Enregistrer un commentaire