LRDPRDX
LRDPRDX

Reputation: 682

Default `const vector<int> &id` argument in function

I have the following function:

double Fitter(double EBeam, vector<KFParticle>
&MeasParticle, vector<TLorentzVector> &RecoParticle,
const vector<int> &id) {  
   do something
}

But sometimes I do no want to provide the last argument (id vector ). I need something like this:

double Fitter( ..., const vector<int> &id = empty_vector )

But I do not want to create some static vector.

Upvotes: 2

Views: 344

Answers (2)

nvoigt
nvoigt

Reputation: 77334

In this case, the answer from @Some programmer dude is perfect (please upvote his if you find it useful):

double Fitter( ..., const vector<int> &id = std::vector<int>{} )

In case you find a case where you cannot implement a default because it's dynamic, you can write an overload:

double Fitter(double EBeam,
              vector<KFParticle> &MeasParticle, 
              vector<TLorentzVector> &RecoParticle,
              const vector<int> &id) 
{  
    // ... do something 
}

double Fitter(double EBeam,
              vector<KFParticle> &MeasParticle, 
              vector<TLorentzVector> &RecoParticle) 
{ 
  vector<int> id; // you could do something dynamic here instead
  return Fitter(EBeam, MeasParticle, RecoParticle, id);
}

Upvotes: 3

Some programmer dude
Some programmer dude

Reputation: 409356

You can default-construct an empty vector inline:

double Fitter( ..., const vector<int> &id = std::vector<int>{} )

Since id is a constant-reference it will work.

Upvotes: 5

Related Questions