我可以使用std::set作为函数的默认参数吗?

时间:2021-10-02 19:31:50

I'm new to this and now sure whether this is doable. I want to add a argument of std::set<std::string> to a function, and set its default value to be NULL, to avoid impact on previous uses.

我是新手,现在确定这是否可行。我想添加std的一个参数::set <:string> 到一个函数,并设置它的默认值为NULL,以避免对以前的使用产生影响。

So basically,

所以基本上,

func(int a); turns into  
func(int a, std::set<std::string> & temp = NULL);

but this will give me an error "error C2440: 'default argument' : cannot convert from 'int' to 'std::set<_Kty> &'"

但这将给我一个错误“C2440:默认参数”:不能从“int”转换为“std::set<_Kty> &”

Can anybody help me on this?

有人能帮我吗?

Thanks

谢谢

4 个解决方案

#1


5  

In order to set the default to NULL, you'd have to be passing an std::set<std::string>*, not a reference to a value type.

为了将默认值设置为NULL,您必须通过std::set <:string> *,而不是一个值类型的引用。

Furthermore, if you are passing a non-pointer type and you want to assign any default value at all, it has to be a const reference, because you can't (advisably!) assign a temporary to it otherwise.

此外,如果您正在传递一个非指针类型,并且您想要分配任何默认值,那么它必须是一个const引用,因为您不能(明智地!)为它分配一个临时的值。

So your choices for "default" values are basically:

所以你对“默认”值的选择基本上是:

std::set<std::string>* = NULL

or:

或者:

const std::set<std::string>& = std::set<std::string>()

or option 3, using function overloading more directly:

或选项3,使用功能重载更直接:

void myfunction() {dothing(0);}
void myfunction(std::set<std::string>& optional_param) 
{ dothing(optional_param.size()); }

or option 4, having a corresponding bool indicating whether parameter is "set":

或选项4,有相应的bool指示参数是否为“set”:

void myfunction(std::set<std::string>& param, bool param_has_meaning=true) {}

It looks like you're already on the track to the third option. You just need to write two definitions, one with and one without the parameter.

看起来你已经在第三个选择的轨道上了。您只需要写两个定义,一个和一个没有参数。

#2


1  

You have the right idea - using a reference. However, a reference cannot be NULL by default, like a pointer can. Therefore, what you probably want to do is overload the function so that you use void func(int a) when you don't want to pass a set as a parameter and use void func( int a, std::set<std::string>& temp)

你有正确的想法——使用参考。但是,默认情况下,引用不能为空,比如指针可以。因此,您可能想要做的是重载函数,以便使用void func(int a),当您不想传递一个集作为参数并使用void func(int a, std::set <:string> & temp)

This way, you can actually provide two separate implementations - one that works on a set and one that doesn't. From a usage point of view, it would have the same effect as a default parameter. From a coding point of view, each implementation would have a clearer purpose.

这样,您实际上可以提供两个单独的实现——一个在集合上工作,另一个不工作。从使用的角度来看,它将具有与默认参数相同的效果。从编码的角度来看,每个实现都有一个更明确的目的。

If you're not going to be modifying the set, might I suggest using a const reference instead:

如果您不打算修改设置,我建议您使用const引用来代替:

void func( int a, const std::set<std::string>& temp )

#3


1  

You can't have a NULL reference in C++.

在c++中不能有空引用。

The simplest way would be to have a dummy empty set:

最简单的方法是使用一个空集:

std::set<std::string> empty;
void func(int a, std::set<std::string>& temp = empty)
{
    // ...
}

You can then call:

然后可以调用:

    func(1);

Neater, still, would be to use function overloading to create a wrapper so that you have no need to default:

Neater仍然会使用函数重载来创建一个包装器,这样您就不必默认了:

void func(int a, std::set<std::string>& temp)
{
}

void func(int a)
{
    std::set<std::string> empty;
    func(a, empty);
}

    // And then...
    func(1);

All this assumes that if you pass in a set you're going to modify it somehow. It's not clear from your question what your intention is but I've made the assumption on the basis that your reference is non-const. If I've miscalculated, then the answer is even simpler:

所有这些都假设,如果你传入一个集合,你将以某种方式修改它。从你的问题不清楚你的意图是什么,但我已经假设你的参考是非常量。如果我算错了,答案就更简单了:

void func(int a, const std::set<std::string>& temp = std::set<std::string>())
{
}

#4


0  

The following will give you an empty set object:

下面将给您一个空集对象:

std::set<std::string>()

#1


5  

In order to set the default to NULL, you'd have to be passing an std::set<std::string>*, not a reference to a value type.

为了将默认值设置为NULL,您必须通过std::set <:string> *,而不是一个值类型的引用。

Furthermore, if you are passing a non-pointer type and you want to assign any default value at all, it has to be a const reference, because you can't (advisably!) assign a temporary to it otherwise.

此外,如果您正在传递一个非指针类型,并且您想要分配任何默认值,那么它必须是一个const引用,因为您不能(明智地!)为它分配一个临时的值。

So your choices for "default" values are basically:

所以你对“默认”值的选择基本上是:

std::set<std::string>* = NULL

or:

或者:

const std::set<std::string>& = std::set<std::string>()

or option 3, using function overloading more directly:

或选项3,使用功能重载更直接:

void myfunction() {dothing(0);}
void myfunction(std::set<std::string>& optional_param) 
{ dothing(optional_param.size()); }

or option 4, having a corresponding bool indicating whether parameter is "set":

或选项4,有相应的bool指示参数是否为“set”:

void myfunction(std::set<std::string>& param, bool param_has_meaning=true) {}

It looks like you're already on the track to the third option. You just need to write two definitions, one with and one without the parameter.

看起来你已经在第三个选择的轨道上了。您只需要写两个定义,一个和一个没有参数。

#2


1  

You have the right idea - using a reference. However, a reference cannot be NULL by default, like a pointer can. Therefore, what you probably want to do is overload the function so that you use void func(int a) when you don't want to pass a set as a parameter and use void func( int a, std::set<std::string>& temp)

你有正确的想法——使用参考。但是,默认情况下,引用不能为空,比如指针可以。因此,您可能想要做的是重载函数,以便使用void func(int a),当您不想传递一个集作为参数并使用void func(int a, std::set <:string> & temp)

This way, you can actually provide two separate implementations - one that works on a set and one that doesn't. From a usage point of view, it would have the same effect as a default parameter. From a coding point of view, each implementation would have a clearer purpose.

这样,您实际上可以提供两个单独的实现——一个在集合上工作,另一个不工作。从使用的角度来看,它将具有与默认参数相同的效果。从编码的角度来看,每个实现都有一个更明确的目的。

If you're not going to be modifying the set, might I suggest using a const reference instead:

如果您不打算修改设置,我建议您使用const引用来代替:

void func( int a, const std::set<std::string>& temp )

#3


1  

You can't have a NULL reference in C++.

在c++中不能有空引用。

The simplest way would be to have a dummy empty set:

最简单的方法是使用一个空集:

std::set<std::string> empty;
void func(int a, std::set<std::string>& temp = empty)
{
    // ...
}

You can then call:

然后可以调用:

    func(1);

Neater, still, would be to use function overloading to create a wrapper so that you have no need to default:

Neater仍然会使用函数重载来创建一个包装器,这样您就不必默认了:

void func(int a, std::set<std::string>& temp)
{
}

void func(int a)
{
    std::set<std::string> empty;
    func(a, empty);
}

    // And then...
    func(1);

All this assumes that if you pass in a set you're going to modify it somehow. It's not clear from your question what your intention is but I've made the assumption on the basis that your reference is non-const. If I've miscalculated, then the answer is even simpler:

所有这些都假设,如果你传入一个集合,你将以某种方式修改它。从你的问题不清楚你的意图是什么,但我已经假设你的参考是非常量。如果我算错了,答案就更简单了:

void func(int a, const std::set<std::string>& temp = std::set<std::string>())
{
}

#4


0  

The following will give you an empty set object:

下面将给您一个空集对象:

std::set<std::string>()