Troubles with templates...
Artyom Shalkhakov
artyom.shalkhakov at gmail.com
Sat Oct 6 10:22:42 PDT 2007
Hello everyone!
Well, here's what I'm trying to do:
module rtparse;
import Integer = tango.text.convert.Integer;
import Float = tango.text.convert.Float;
import Text = tango.text.Util;
// parse an integer out of string
void parse( T : int, U )( ref int dst, U[] src ) {
dst = Integer.toInt( src );
}
// generalized parsing function, just a stub
// but if it's not present, DMD complains:
// template parse(T : int, U) does not match any template declaration
// template parse(T : int, U) cannot deduce template function from argument types (int, char[])
void parse( T, U )( ref T dst, U[] src ) {
// static if ( is( T == int ) ) {
// dst = Integer.toInt( src );
// }
}
struct ParseReturn {
uint count; // count of parsed arguments
uint ate; // characters eaten
}
ParseReturn scanDelimited( T, Args... )( T[] src, T[] set, out Args args ) {
static assert( is( void function( Args ) ), "scanDelimited :: Args should be a type tuple" );
uint pos, mark;
T[] get() {
T[] token;
if ( set.length is 1 ) {
// use fast path if delimiter is a single char
if ( ( pos = Text.locate( src, set[0], mark ) ) < src.length ) {
token = src[mark..pos];
mark = pos + 1;
return token;
}
}
else if ( set.length > 1 ) {
foreach ( i, elem; src ) {
if ( Text.contains( set, elem ) ) {
token = src[mark..i];
mark = i + 1;
return token;
}
}
}
return src[mark..$]; // NOTE: empty tokens are allowed
}
uint count;
foreach ( i, arg; args ) {
T[] token = get();
// this calls parse( T, U ) instead of parse( T : int, U )
parse( arg, token );
count++;
}
return ParseReturn( count, pos ); // beyond return, args are not copied!
}
import tango.io.Stdout;
void main() {
ParseReturn r;
{
int a;
// seems like an issue with tuple and ref/inout/out modifiers
scanDelimited( "1", ":", a );
// 'a' doesn't return from the function;
// in Ddbg, I see that 'arg' value (local in scanDelimited)
// gets a value of 1, but in this statement, 'a' is 0!
assert( a == 1 );
}
// more tests omitted
}
I'm using tango0.99 RC3 + DMD1.21.
What do I do wrong?
Best regards.
More information about the Digitalmars-d-learn
mailing list