mardi 4 août 2015

Default value for a reference parameter to a function

I have a function that takes two parameter. The first parameter is an int& which the function will set to some "return" value. The second parameter is a pointer and is optional. The the caller can pass a valid pointer if the caller wishes it to be initialized, and if not the default value is nullptr.

void find_lis(uint32_t& count,
              vector<uint32_t>* output = nullptr)
{ }

All is well. However, I want to make the second parameter a reference, and allow caller the same option to provide one or not, what should I use as the default value?

void find_lis(uint32_t& count,
              vector<uint32_t>* output = ???)
{ }

I tried a few things, but getting compiler error. However, the following at least compiles, but not sure if it is right?

void find_lis(uint32_t& count,
              vector<uint32_t>& output = *(new vector<uint32_t>()))
{ }

Unlike in the case of pointer type, I an easily check if the caller passed in a second parameter by comparing value to nullptr. However, in the case of reference, I don't see any such easy check.

Would appreciate any thoughts. Thank you in advance.

Aucun commentaire:

Enregistrer un commentaire