I've started making use of std::unique_ptr
e.g.:
unique_ptr<TFile> myfile( TFile::Open("myfile.root") );
instead of
TFile * myoldfile = TFile::Open("myoldfile.root") ;
Now I'm not sure what my functions should look like. I think part of the problem might be that my code is not complicated enough yet for any problems to become apparent but I'd like to get it right now so I don't end up in a mess when things are more complicated.
I used to have:
double interestingResult( TFile * input )
{...}
(which doesn't actually modify TFile but can't be const because it calls some non-const functions of TFile).
I could still call this by doing:
myResult = interestingResult( myfile.get() );
which doesn't seem very user friendly. (I think it is suggested here: https://stackoverflow.com/a/5325560/1527126 .)
Or I could modify my function to look like:
double interestingResult( unique_ptr<TFile>& input )
{...}
which forces the user to always use a unique_ptr.
or I could support both by writing:
double interestingResult( unique_ptr<TFile>& input )
{ return interestingResult( intput.get() ); }
but I don't see that in other people's code.
What is the standard approach to this situation?
I think this answer (https://stackoverflow.com/a/9700189/1527126) implies I should accept references since I don't expect myfile
to be null. But the library function TFile::Open
always returns a pointer so it seems natural to be able to pass that straight into the function without extra dereferencing.
Apologetic p.s. I gave up trying to work out whether I should ask on StackOverflow or CodeReview or not at all but please point me to the appropriate place if this is not it.