Optional out parameter of function

monarch_dodra monarchdodra at gmail.com
Wed Nov 27 03:14:51 PST 2013


On Wednesday, 27 November 2013 at 10:16:13 UTC, Jonathan M Davis 
wrote:
> On Wednesday, November 27, 2013 10:57:42 Joseph Rushton 
> Wakeling wrote:
>> On 27/11/13 10:45, bearophile wrote:
>> > It's useless code, you can't have ref variadic, sorry.
>> 
>> Ack. :-(  I just tried it out myself and found the same thing.
>
> Personally, I think that it's by far the best approach to just 
> do
>
>      int foo(int i)
>      {
>          int j;
>          return foo(i, j);
>      }
>
> It's clean, and I really don't see a big problem with it. But 
> if you _really_
> don't want to do that, you can always create a dummy variable 
> and do something
> like
>
> int dummy;
>
> int foo(int i, out int j = dummy)
> {
>     ...
> }
>
> But I'd advise against it, since it strikes me as rather messy, 
> and I really
> don't see any problem with just creating a wrapper function. 
> It's the kind of
> thing that you already have to pretty much any time that you 
> try and have a
> function accept const ref for an efficiency boost, since ref 
> doesn't accept
> rvalues. A bit annoying perhaps, but not a big deal.
>
> - Jonathan M Davis

Interesting trick. That said, doing this (should) also mean you 
can't use foo in a pure context, since it means the caller needs 
to access the global dummy (although that seems to work right 
now).


More information about the Digitalmars-d-learn mailing list