[英]C++ Optional std::vector argument for function
I have seen some answers here, but they may not apply here. 我在这里看到了一些答案,但它们可能不适用于这里。 I have a (member) function that is mostly used with only one argument (the 1st):
我有一个(成员)函数,通常只使用一个参数(第一个):
const std::complex<double> Class::func(const std::complex<double> &x, \
std::vector<double> &y = 0, \
std::vector<double> &z = 0) const;
I would like for y
and z
to be optional, maybe even based on a fourth argument of type string
, something like this: func( , , , const std::string &choice)
, but which would let me only pass one argument to the function and the other two not be used. 我希望
y
和z
是可选的,甚至可能基于string
类型的第四个参数,如下所示: func( , , , const std::string &choice)
,但是这样我只能将一个参数传递给功能,其他两个不使用。 If I do pass y
, for example, its declaration would have to be done previous of calling func()
, same for z
, but I'd like this to be optional, the function can be a bit heavy on the math side and there's no need to add the burden of calculating two additional vectors if they're not needed. 例如,如果我确实传递了
y
,则它的声明必须在调用func()
之前进行,与z
相同,但我希望此声明是可选的,该函数在数学方面可能会有点沉重,并且如果不需要,则无需增加计算两个附加向量的负担。 Is this possible? 这可能吗?
Use pointers and pass a nullptr
, check for this.. 使用指针并传递一个
nullptr
,检查一下。
const std::complex<double> Class::func(const std::complex<double> &x, \
std::vector<double> *y = nullptr, \
std::vector<double> *z = nullptr) const;
I've defaulted them here, but that's optional... 我已经在这里默认了它们,但这是可选的...
Given the use of the std::vector
however, what does an "empty" vector
mean? 给定
std::vector
的使用,“空” vector
是什么意思? This could be used as the "optional" value you require. 可以将其用作您所需的“可选”值。
Some notes on the original code; 关于原始代码的一些注释; the "null" reference is not valid, ie
std::vector<double> &y = 0
does not compile. “空”引用无效,即
std::vector<double> &y = 0
不会编译。 A pointer could be used in it place, it would not immediately mean allocation are required. 可以在其中使用指针,这并不意味着立即需要分配。
const std::complex<double> Class::func(
const std::complex<double> &x,
std::vector<double>* y = nullptr,
std::vector<double>* z = nullptr) const;
Function overloads are often an answer for situation such as this; 函数重载通常可以解决这种情况。 the internals of the functions are defer to each for the final implementation...
函数的内部结构取决于最终实现...
const std::complex<double> Class::func(
const std::complex<double> &x) const;
const std::complex<double> Class::func(
const std::complex<double> &x,
std::vector<double>& y) const;
const std::complex<double> Class::func(
const std::complex<double> &x,
std::vector<double>& y,
std::vector<double>& z) const;
There is also std::optional
. 还有
std::optional
。
const std::complex<double> Class::func(
const std::complex<double> &x,
std::optional<std::vector<double>> &y,
std::optional<std::vector<double>> &z) const;
If not available in your standard library, there is also boost::optional
. 如果您的标准库中没有,则还有
boost::optional
。
References cannot be optional but there is something called boost::optional<>
引用不能是可选的,但是有一个称为
boost::optional<>
If you are concerned about copy-operations you should use a pointer instead and use nullptr
as default. 如果您担心复制操作,则应改用指针并将
nullptr
用作默认值。
If you also want to make it dependent on a string at the end you could write a wrapper function 如果您还想使它最后依赖于字符串,则可以编写包装函数
Just add another function: 只需添加另一个功能:
const std::complex<double> Class::func(
const std::complex<double> &x,
std::string str,
std::vector<double> *y = nullptr,
std::vector<double> *z = nullptr) const
{
if(str == ...)
return func(....);
else
return func(....); //different call
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.