简体   繁体   English

如何在D中为函数的参考参数创建默认值?

[英]How to create default value for reference argument for function in D?

Signature of my function: 我的功能签名:

JSONValue get(string data, ref int parentCounter)

The second argument must be passed by reference and to be optional. 第二个参数必须通过引用传递,并且是可选的。 My nonworking variants: 我的无效版本:

JSONValue get(string data, ref int parentCounter = 10);
JSONValue get(string data, ref int parentCounter = int(10));
JSONValue get(string data, ref int parentCounter = new int(10));
JSONValue get(string data); // override also does not work

DMD32 D Compiler v2.065 DMD32 D编译器v2.065

As the argument is ref , it must have an address. 由于参数是ref ,因此必须有一个地址。 Therefore, it must be an lvalue expression. 因此,它必须是一个左值表达式。

You can do this: 你可以这样做:

JSONValue get(string data, ref int parentCounter = *new int);

You currently can't use this syntax to also give the newly-allocated int a value. 您目前无法使用此语法来给新分配的int一个值。 But, in D 2.066, you'll be able to write: 但是,在D 2.066中,您可以编写:

JSONValue get(string data, ref int parentCounter = *new int(10));

This will allocate a new int on the heap, unless one is specified at the caller site. 除非在调用者站点上指定了一个新的int ,否则它将在堆上分配一个新的int

You can also use a static variable, or a ref function call: 您还可以使用静态变量或ref函数调用:

int defaultValue;

ref int defaultValueFun()
{
    auto i = new int;
    *i = 10;
    return *i;
}

JSONValue get(string data, ref int parentCounter = defaultValue);
// or
JSONValue get(string data, ref int parentCounter = defaultValueFun());

Be wary of this technique if defaultValue might be called while a reference to its value is still in use, though. 警惕这种技术如果defaultValue可能被称为,而其参考value仍在使用,虽然。

Probably the simplest solution at this point would be to just overload the function. 这时最简单的解决方案可能就是重载该函数。 eg 例如

JSONValue get(string data)
{
    int dummy = 10;
    return get(data, dummy);
}

JSONValue get(string data, ref int parantCounter)
{
    ...
}

It also avoids any unnecessary heap allocations, unlike cybershadow's suggestion of using *new int(10) once 2.066 is out (though being able to do *new int(10) will definitely be cool). 它也避免了任何不必要的堆分配,这与Cyber​​shadow建议在2.066退出后使用*new int(10)的建议不同(尽管能够进行*new int(10)肯定很酷)。

Now, you seem to be indicating in your question that for some reason, overloading the function doesn't work: 现在,您似乎在问题中指出由于某种原因,重载该功能不起作用:

JSONValue get(string data); // override also does not work

So, maybe this solution won't work for you, but without more information, I don't know why it wouldn't. 因此,也许该解决方案对您不起作用,但是如果没有更多信息,我不知道为什么不行。 Certainly, if you're dealing with free functions, it will, and if you're dealing with struct member functions, it will. 当然,如果您要处理的是自由函数,则可以,如果您要处理的是struct成员函数,则可以。 The only possible problem that I can think of is if you're overriding a base class function, but even then, the only problem I can think of is if the base class declares 我能想到的唯一可能的问题是,如果您要重写基类函数,但是即使如此,我能想到的唯一问题是,如果基类声明了

JSONValue get(string data, ref int parentCounter)

and you're trying to call the function via the base class rather than derived class, and if you're doing that, then having a default argument in the overridden function wouldn't help you any anyway, because the base class didn't declare one - the default argument would only work when used via the derived class. 并且您试图通过基类而不是派生类来调用该函数,并且如果这样做,那么无论如何,在重写函数中使用默认参数都无济于事,因为基类没有声明一个-默认参数仅在通过派生类使用时才有效。 Certainly, you could override the base class' get and then add an overload in the derived class that looks like 当然,您可以覆盖基类的get ,然后在派生类中添加一个如下所示的重载:

JSONValue get(string data)

So, if declaring an overload like that isn't working for you, I'll need more details in order to help you figure out why it isn't working. 因此,如果声明这样的重载对您不起作用,我将需要更多详细信息,以帮助您弄清为什么它不起作用。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM