foreach over split string

JS js.mdnq at gmail.com
Thu Jul 18 14:57:30 PDT 2013


On Thursday, 18 July 2013 at 19:11:55 UTC, H. S. Teoh wrote:
> On Thu, Jul 18, 2013 at 12:07:58PM +0200, JS wrote:
>> Since you do such a good job a explaining things, would you 
>> mind
>> informing me what the difference between alias T, T and string 
>> T
>> used as template parameters are?
>> 
>> template A!(T) { }
>
> T here is a type name. So you can pass in 'int', 'string', 
> etc., or any
> user-defined type.
>
>
>> template A!(alias T) { }
>
> T here is a "symbol", that is, the name of a variable, or a 
> function
> literal, etc. Basically an entry in the compiler's symbol table.
>
>
>> template A!(string T) { }
> [...]
>
> T here is a string value. So either a string literal or a 
> string value
> known at compile-time. Note that this should not be confused 
> with the
> string *type*. That is:
>
> 	A!("abc")	// matches template A!(string T)
> 	A!(string)	// matches template A!(T)
>
> It's probably not a good idea to overload a template on an alias
> parameter *and* a string parameter, though, because alias can 
> refer to
> *any* symbol, including string literals. You'll either get an 
> ambiguity
> error, or the compiler may do something unexpected.
>
> You're probably asking what "any symbol" includes. Perhaps this 
> code
> might be enlightening:
>
> 	template Templ(alias A) {
> 		pragma(msg, typeof(A));
> 		enum Templ = 1;
> 	}
>
> 	void main() {
> 		int x;
> 		int delegate(int x) dg;
>
> 		auto dummy1 = Templ!("a");
> 		auto dummy2 = Templ!(x);
> 		auto dummy3 = Templ!((int z) => z*2);
> 		auto dummy4 = Templ!(dg);
> 		auto dummy5 = Templ!(123);
>
> 		// Compile error: types are not symbols, so they won't
> 		// match the alias parameter:
> 		//auto dummy6 = Templ!(int);
> 	}
>
> The compiler output is:
>
> 	string
> 	int
> 	int function(int z) pure nothrow @safe
> 	int delegate(int x)
> 	int
>
> That is to say, "alias A" picked up everything ranging from 
> local
> variables to string literals to function literals. The only 
> thing it
> didn't pick up is types.
>
> (As to *why* alias parameters behave in this way and why that 
> might be
> useful, that's a whole 'nother topic. :))
>
>


You did it again! Thanks for the accurate and details response! I 
don't mind there being a difference between T and alias T as long 
as I know what it is ;) (although it does require two templates 
do deal with both cases and can increase template explosion).

Thank again.



More information about the Digitalmars-d-learn mailing list