函数的引用参数的默认值

时间:2022-05-09 17:07:17

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.

我有一个函数,它取两个参数。第一个参数是int&,函数将其设置为某个“return”值。第二个参数是指针,是可选的。如果调用者希望初始化一个有效的指针,并且如果没有的话,默认值是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.

与指针类型不同的是,我可以通过将值与nullptr进行比较,轻松地检查调用者是否传入了第二个参数。但是,就参考文献而言,我没有看到任何这样简单的检查。

Would appreciate any thoughts. Thank you in advance.

将不胜感激任何想法。提前谢谢你。

2 个解决方案

#1


3  

If the implementation of the function is not overly complex, I would suggest creating two overloaded functions.

如果函数的实现不是过于复杂,我建议创建两个重载的函数。

void find_lis(uint32_t& count)
{
   count = 0; // ???
}

void find_lis(uint32_t& count,
              vector<uint32_t>& output)
{
   // Proper implementation
}

Another option:

另一个选择:

void find_lis(uint32_t& count)
{
   // I'm guessing this will work. May be not.
   static vector<uint32_t> dummy;
   find_lis(count, dummy);
}

void find_lis(uint32_t& count,
              vector<uint32_t>& output)
{
   // Proper implementation
}

Update, in response to comment by OP

更新,以回应OP的评论。

Use:

使用:

void find_lis_private(uint32_t& count,
                      vector<uint32_t>& output,
                      bool update)
{
   // Proper implementation
}

void find_lis(uint32_t& count)
{
   static vector<uint32_t> dummy;
   find_lis_private(count, dummy, false);
}

void find_lis(uint32_t& count,
              vector<uint32_t>& output)
{
   find_lis_private(count, output, true);
}

A better option is to use:

更好的选择是:

template <typename UpdateFunction>
void find_lis_private(uint32_t& count,
                      vector<uint32_t>& output,
                      UpdateFunction fun)
{
   // Proper implementation
   // call fun() with the necessary arguments when it's time
   // to update.
}

void find_lis(uint32_t& count)
{
   static vector<uint32_t> dummy;
   find_lis_private(count, dummy, [](args...) {/* Empty function */});
}

void find_lis(uint32_t& count,
              vector<uint32_t>& output)
{
   find_lis_private(count, output, [=output](args...) {/* Update output */});
}

Then, you don't have to use if/else blocks to update.

然后,您不必使用if/else块进行更新。

#2


2  

If you really want to bind a temporary to an lvalue reference, you can use a helper function to convert the rvalue to an lvalue:

如果您真的想将一个临时值绑定到一个lvalue引用,您可以使用一个helper函数将rvalue转换为一个lvalue:

template <class T>
T& lvalue_cast(T&& t)
{
    return t;
}

void find_lis(uint32_t& count,
              std::vector<uint32_t>& output = lvalue_cast(std::vector<uint32_t>()))
{
}

#1


3  

If the implementation of the function is not overly complex, I would suggest creating two overloaded functions.

如果函数的实现不是过于复杂,我建议创建两个重载的函数。

void find_lis(uint32_t& count)
{
   count = 0; // ???
}

void find_lis(uint32_t& count,
              vector<uint32_t>& output)
{
   // Proper implementation
}

Another option:

另一个选择:

void find_lis(uint32_t& count)
{
   // I'm guessing this will work. May be not.
   static vector<uint32_t> dummy;
   find_lis(count, dummy);
}

void find_lis(uint32_t& count,
              vector<uint32_t>& output)
{
   // Proper implementation
}

Update, in response to comment by OP

更新,以回应OP的评论。

Use:

使用:

void find_lis_private(uint32_t& count,
                      vector<uint32_t>& output,
                      bool update)
{
   // Proper implementation
}

void find_lis(uint32_t& count)
{
   static vector<uint32_t> dummy;
   find_lis_private(count, dummy, false);
}

void find_lis(uint32_t& count,
              vector<uint32_t>& output)
{
   find_lis_private(count, output, true);
}

A better option is to use:

更好的选择是:

template <typename UpdateFunction>
void find_lis_private(uint32_t& count,
                      vector<uint32_t>& output,
                      UpdateFunction fun)
{
   // Proper implementation
   // call fun() with the necessary arguments when it's time
   // to update.
}

void find_lis(uint32_t& count)
{
   static vector<uint32_t> dummy;
   find_lis_private(count, dummy, [](args...) {/* Empty function */});
}

void find_lis(uint32_t& count,
              vector<uint32_t>& output)
{
   find_lis_private(count, output, [=output](args...) {/* Update output */});
}

Then, you don't have to use if/else blocks to update.

然后,您不必使用if/else块进行更新。

#2


2  

If you really want to bind a temporary to an lvalue reference, you can use a helper function to convert the rvalue to an lvalue:

如果您真的想将一个临时值绑定到一个lvalue引用,您可以使用一个helper函数将rvalue转换为一个lvalue:

template <class T>
T& lvalue_cast(T&& t)
{
    return t;
}

void find_lis(uint32_t& count,
              std::vector<uint32_t>& output = lvalue_cast(std::vector<uint32_t>()))
{
}