Why can't templates with default arguments be instantiated	without the bang syntax?
    Jonathan M Davis 
    jmdavisProg at gmx.com
       
    Thu Sep 15 08:57:42 PDT 2011
    
    
  
On Thursday, September 15, 2011 16:46:24 Andrej Mitrovic wrote:
> struct Foo(T = int) {}
> 
> void main()
> {
>     Foo foo;  // fail
>     Foo!() bar;  // ok
> }
> 
> It would be very convenient to be able to default to one type like this.
> 
> For example, in CairoD there's a Point structure which takes doubles
> as its storage type, and then there's PointInt that takes ints. The
> reason they're not both a template Point() that takes a type argument
> is because in most cases the user will use the Point structure with
> doubles, and only in rare cases Point with ints. So to simplify code
> one doesn't have to write Point!double in all of their code, but
> simply Point.
> 
> If the bang syntax wasn't required in presence of default arguments
> then these workarounds wouldn't be needed.
There is no type inference for templated structs, so you need the bang. Only 
functions get type inference. They way to fix this is to create a separate 
factory function to construct the type. std.container does this with 
redBlackTree for RedBlackTree.
- Jonathan M Davis
    
    
More information about the Digitalmars-d-learn
mailing list