some template questions

Daniel Keep daniel.keep.lists at gmail.com
Tue May 23 01:42:27 PDT 2006



BCS wrote:
> In article <e4ub7s$2v7l$1 at digitaldaemon.com>, Don Clugston says...
>> BCS wrote:
>>> Two questions:
> [...]
>> It doesn't work because D doesn't have array literals yet.
> 
> ???
> 
> this works (I was using it tonight):
> 
> struct fo {int i}
> fo[] bar = [ {i:5}, {i:6} ];
> 
> --------------------------
>>> template hang()
>>> {
>>> static assert(false);
>>> const int hang = hang!();
>>> }
> [...]
>> -----
>> If that it fails for you, enter it in Bugzilla as a regression or 
>> Linux-only bug.
>>
> 
> @#$%@ faulty memory (the gray wet stuff, not the silicon suff). Try:
> 
> template hang(int i)
> {
> static assert(0);
> const int hang = hang!(i-1);
> }
> 
> const int x = hang!(1);
> 
> this hangs 0.157 on Linux and XP (now bug #152)
> 
> 
> OK, next question: has anyone done a sort?
> 
> static assert("abc" == sort!("bca"));
> 

Here you go:

#
# template insertChar(char[] chr, char[] str)
# {
#     static if( str.length == 0 )
#         const char[] insertChar = chr;
#     else static if( chr[0] <= str[0] )
#         const char[] insertChar = chr ~ str;
#     else
#         const char[] insertChar = str[0..1] ~ insertChar!(chr, #
str[1..$]);
# }
#
# template sort(char[] str)
# {
#     static if( str.length == 0 )
#         const char[] sort = "";
#     else
#         const char[] sort = insertChar!(str[0..1], sort!(str[1..$]));
# }
#
# template slice(char[] str)
# {
#     const char[] slice = str[1..$-1];
# }
#
# pragma(msg, `sort!("bsakdjfyiu4r") == ` ~ sort!("bsakdjfyiu4r"));
#
# void
# main()
# {
# }
#

Tested it just then :)

	-- Daniel Keep

-- 

v1sw5+8Yhw5ln4+5pr6OFma8u6+7Lw4Tm6+7l6+7D
a2Xs3MSr2e4/6+7t4TNSMb6HTOp5en5g6RAHCP    http://hackerkey.com/



More information about the Digitalmars-d-learn mailing list