Out parameters and initialization

Thomas Kuehne thomas-dloop at kuehne.cn
Thu Feb 23 23:32:02 PST 2006


-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1

Unknown W. Brackets schrieb am 2006-02-20:
> 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.)

Where exactly are default parameters for functions documented?
changelog:
| Added default arguments to function parameters. Semantics are like C++.

functions:
| A functions parameter's default value is not inherited.

C++ has - as far as I'm aware - no out keyword.

Added to DStress as
http://dstress.kuehne.cn/undefined/default_argument_10.d

Thomas


-----BEGIN PGP SIGNATURE-----

iD8DBQFD/rXF3w+/yD4P9tIRAnagAJ49GTZBvwbg4gQG3x2vweQsCJsTbwCguviK
tP/CaKTY4XUpBuCgkyI0Xu8=
=mkMH
-----END PGP SIGNATURE-----



More information about the Digitalmars-d-bugs mailing list