Domanda

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

È stato utile?

Soluzione

As the argument is ref, it must have an address. 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. But, in D 2.066, you'll be able to write:

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.

You can also use a static variable, or a ref function call:

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.

Altri suggerimenti

Probably the simplest solution at this point would be to just overload the function. e.g.

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).

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. 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

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.

Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top