Instantiating Templates Structs with defaults parameter

Jens Mueller jens.k.mueller at gmx.de
Thu Jun 9 07:55:15 PDT 2011


d coder wrote:
> Greetings
> 
> Kindly look at the code below. When I try to compile it with DMD 2-53, I get
> an error:
> 
> foo.d(9): Error: struct template.Foo(int n = 1) is used as a type
> 
> It would be convenient if I am allowed to use Foo as a type without passing
> parameters (with parameterized functions that is the default behavior). I
> want to understand if not being able to instantiate Foo with default
> parameters is a bug in DMD? Otherwise If it is not allowed in D2, I want to
> know why such a limitation is there.
> 
> Regards
> - Puneet
> 
> import std.stdio;
> 
> struct Foo(int n = 1) {
>   public int value = n;
> }
> 
> void main()
> {
>   Foo foo; // Does not compile
>   // Foo!() foo; // This works
>   writeln("Value is, ", foo.value);
> }

I don't know whether this should compile but there is a simple work
around:

alias Foo!() Bar;

and then

Bar bar;

works as expected.
Does it help?

Jens


More information about the Digitalmars-d mailing list