January 14, 2006 Re: Passing string literals to templates? | ||||
---|---|---|---|---|
| ||||
Garett Bass wrote: > I'm trying to use a template parameterized with a string literal, but I'm not sure of the syntax. I didn't find any examples in the D documentation on templates. Help please? Try this: > ----------- > module test; > private import std.stdio; > > class Base(S) { // you want to use a string value parameter class Base(char[] S) { > static char[] name = S; > char[] toString() { return name; } > } > > class Derived1 : public Base!("Derived1") { // Line 9 > } > > class Derived2 : public Base!("Derived2") { // Line 12 > } > > void main() { > Base b1 = new Derived1; // 'Base' isn't the full type name for base, it's // 'Base!(...)'. Instead, use Derived1 or define // a non-template nase class or interface. Derived1 b1 = new Derived1; > writefln(b1); > > Base b2 = new Derived2; Derived2 b2 = new Derived2; > writefln(b1); > } Sean |
Copyright © 1999-2021 by the D Language Foundation