Out parameters and initialization

Unknown W. Brackets unknown at simplemachines.org
Sun Feb 19 17:53:45 PST 2006


The spec says:

out parameters are set to the default initializer for the type of it.

Which makes perfect sense, and works just fine.  The trick comes in when 
I try some parameter initialization.  This really weirded me out at 
first.  Example:

int main()
{
         uint i = 0;
         test(i);

         return 0;
}

void test(out uint i = 5)
{
         assert(i == 5);
}

It seems to me that I should either get an error from the compiler, or 
this should compile and run without any assertions failing.  However, 
the assert on line 11 does not pass, as of DMD 0.147.

Either way, I would suggest the sentence in the spec be clarified (and 
the grammar buffed), for example:

out parameters are always set to the default initializer for their type. 
  Using an initializer on an out parameter is an error.

Or:

out parameters are always set to the default initializer for their type, 
unless they have an explicit initializer (but regardless of the 
parameters initial value.)

I couldn't find this reported before, but if it has been please forgive 
the duplicate.

Hope that's helpful,
-[Unknown]



More information about the Digitalmars-d-bugs mailing list