no-arg constructor for structs (again)

Jonathan M Davis jmdavisProg at gmx.com
Thu Sep 20 02:09:17 PDT 2012


On Thursday, September 20, 2012 10:11:41 Felix Hufnagel wrote:
> On Thursday, 20 September 2012 at 00:14:04 UTC, Jonathan M Davis
> 
> wrote:
> > On Thursday, September 20, 2012 00:12:04 Felix Hufnagel wrote:
> >> isn't it even worse?
> >> 
> >> import std.stdio;
> >> struct S
> >> {
> >> int i;
> >> this(void* p = null){this.i = 5;}
> >> }
> >> void main()
> >> {
> >> //S l(); //gives a linker error
> >> auto k = S();
> >> writeln(k.i); //prints 0
> >> }
> > 
> > Of course that generates a linker error. You just declared a
> > function without
> > a body.
> > 
> > - Jonathan M Davis
> 
> sure, but it's a bit unexpected. do we need to be able to declare
> empty functions?

It can be useful at module scope, and it would complicate the grammar to make 
it anything else at function scope, even if there's no practical reason to use 
it that way there. C/C++ (which doesn't have nested functions) also treats 
that declaration as a function declaration.

> but whats even more confusing: you are not allowed to declare an
> no_arg constructor. but you are allowed to declare one where all
> parameters have default parameters. but then, how to call it
> without args? auto k = S(); doesn't work?

It's a bug. I'm pretty sure that there's a bug report for it already, but I'd 
have to go digging for it to know which one it is.

- Jonathan M Davis


More information about the Digitalmars-d mailing list