C++ vector<int>& 的默认参数?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/3147274/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-28 12:10:43  来源:igfitidea点击:

C++ Default argument for vector<int>&?

c++arguments

提问by voteforpedro

I have a function,

我有一个功能,

void test( vector<int>& vec );

How can I set the default argument for vec ? I have tried

如何为 vec 设置默认参数?我试过了

void test( vector<int>& vec = vector<int>() );

But there's a warning "nonstandard extension used : 'default argument' : conversion from 'std::vector<_Ty>' to 'std::vector<_Ty> &'"

但是有一个警告“使用了非标准扩展:‘默认参数’:从‘std::vector<_Ty>’转换为‘std::vector<_Ty> &’”

Is there a better way to do this ? Instead of

有一个更好的方法吗 ?代替

void test() {
    vector<int> dummy;
    test( dummy );
}

Regards, Voteforpedro

问候, Voteforpedro

回答by ereOn

Have you tried:

你有没有尝试过:

void test(const vector<int>& vec = vector<int>());

C++does not allow temporaries to be bound to non-const references.

C++不允许临时对象绑定到非常量引用。

If you really to need to have a vector<int>&(not a constone), you can declare a static instance and use it as a default (thus non-temporary) value.

如果您真的需要一个vector<int>&(不是const一个),您可以声明一个静态实例并将其用作默认(因此非临时)值。

static vector<int> DEFAULT_VECTOR;

void test(vector<int>& vec = DEFAULT_VECTOR);

But beware, because DEFAULT_VECTOR will (can) be modified and won't reset on each call ! Not sure that this is what you really want.

但要注意,因为 DEFAULT_VECTOR 将(可以)被修改并且不会在每次调用时重置!不确定这是否是您真正想要的。



Thanks to stinky472, here is a thread-safe alternative:

感谢stinky472,这是一个线程安全的替代方案:

Instead of providing a default value, you might as well overload test()with a zero-parameter version which calls the other version:

与其提供默认值,不如test()使用调用另一个版本的零参数版本进行重载:

void test()
{
  vector<int> vec;
  test(vec);
}

回答by sbi

I find it questionable for a non-constreference argument to have a default value. What is this supposed to mean?

我发现非const引用参数具有默认值是有问题的。这是什么意思?

Commonly, a function taking a non-constreference means "I might change the argument". If the argument is optionalwhy no pass a (non-const) pointer? That way, when callers don't want to pass an arguments, they can pass NULL.
(See herefor more information on how to pass function arguments.)

通常,采用非const引用的函数意味着“我可能会更改参数”。如果参数是可选的,为什么不传递(非const)指针?这样,当调用者不想传递参数时,他们可以传递NULL.
(有关如何传递函数参数的更多信息,请参见此处。)

Edit:Well, and of course, there's also overloading. Just add another overload which doesn't take this argument.

编辑:嗯,当然,还有重载. 只需添加另一个不接受此参数的重载。

回答by stinky472

We cannot initialize mutable references to temporaries. Only const references allow this. What you are after is most likely this:

我们无法初始化对临时对象的可变引用。只有 const 引用允许这样做。你所追求的很可能是这样的:

void test( const vector<int>& vec = vector<int>() );

Aside from avoiding undefined behavior, this makes the most sense logically and from a const-correctness perspective. If you wanted 'test' to modify the original vector being passed to it, you would not have been able to sensibly provide a default value. Thus it's obvious you are using 'vec' here for read-only purposes and should therefore make it a const reference.

除了避免未定义的行为之外,这在逻辑上和从常量正确性的角度来看是最有意义的。如果您希望 'test' 修改传递给它的原始向量,您将无法明智地提供默认值。因此,很明显,您在此处将 'vec' 用于只读目的,因此应将其设为 const 引用。

回答by JohnJohn

I know this question was asked in 2010 but for C++11 you can do it like this:

我知道这个问题是在 2010 年提出的,但对于 C++11,你可以这样做:

void test( vector<int>& vec = {}) {
      // Awesome code here
}

As pointed out in this answer.

正如在这个答案中指出的那样。

回答by Ahmed Hamdy

You could just make pass a pointer to a vector and initialize it to NULL

您可以只传递一个指向向量的指针并将其初始化为 NULL

void test( vector<int>* vec = NULL );