May 03, 2004 template bug (0.86) | ||||
|---|---|---|---|---|
| ||||
DMD 0.86 (run under XP)
I've run into problems with typedefs and templates, it appears that D seems to
see the typedef as the base type.
if you hide the int,int instance of the template then it works
(and gives the correct R.init value)
----------------- bugtest.d ----------------------
class HandleArray( H ) {
protected:
alias H HandlerType;
H[] handlers;
bit findResult( bit delegate(H) predicate ) {
foreach( HandlerType handler; handlers ) {
if ( predicate( handler ) ) { return true; }
}
return false;
}
public:
void opCatAssign( H handler ) {
handlers ~= handler;
}
}
class FuncHandler( R, P ) : HandleArray!( bit delegate( out R, P ) ) {
public R opCall( P p ) {
R rv = R.init;
findResult( delegate bit( HandlerType h ) { return h( rv, p ); } );
return rv;
}
}
///-- example test code;
class IntScanner : FuncHandler!( int, int ) {}
typedef int OddInt = -1;
void test1() {
IntScanner scan = new IntScanner();
bit scanFor1( out int rv, int val ) { if ( val == 1 ) { rv = 10; return true; }
return false; }
scan ~= &scanFor1;
for ( int i = 0; i < 2; i++ ) {
printf( "scan(%d) = %d\n", i, scan( i ) );
}
}
void test2() {
FuncHandler!( OddInt, int ) scan;
scan = new typeof(scan)();
bit scanFor1( out OddInt rv, int val ) { if ( val == 1 ) { rv = cast(OddInt)10;
return true; } return false; }
scan ~= &scanFor1;
for ( int i = 0; i < 2; i++ ) {
printf( "scan<OddInt>(%d) = %d\n", i, scan( i ) );
}
}
int main( char[][] args ) {
test1();
test2();
return 0;
}
//
// bugtest.d(21): function findResult (bit delegate(bit delegate(out
int,int))predi
// cate) does not match argument types (bit delegate(bit delegate(out OddInt
,int))
// )
//
Mike.
| ||||
May 04, 2004 Re: template bug (0.86) | ||||
|---|---|---|---|---|
| ||||
Posted in reply to one_mad_alien | Bugs in the compiler or phobos should be posted to digitalmars.D.bugs <one_mad_alien@hotmail.com> wrote in message news:c76j8u$nqp$1@digitaldaemon.com... > DMD 0.86 (run under XP) > > I've run into problems with typedefs and templates, it appears that D seems to > see the typedef as the base type. > if you hide the int,int instance of the template then it works > (and gives the correct R.init value) > > ----------------- bugtest.d ---------------------- > class HandleArray( H ) { > protected: > alias H HandlerType; > H[] handlers; > bit findResult( bit delegate(H) predicate ) { > foreach( HandlerType handler; handlers ) { > if ( predicate( handler ) ) { return true; } > } > return false; > } > public: > void opCatAssign( H handler ) { > handlers ~= handler; > } > } > > class FuncHandler( R, P ) : HandleArray!( bit delegate( out R, P ) ) { > public R opCall( P p ) { > R rv = R.init; > findResult( delegate bit( HandlerType h ) { return h( rv, p ); } ); > return rv; > } > } > ///-- example test code; > class IntScanner : FuncHandler!( int, int ) {} > > typedef int OddInt = -1; > > > void test1() { > IntScanner scan = new IntScanner(); > bit scanFor1( out int rv, int val ) { if ( val == 1 ) { rv = 10; return true; } > return false; } > scan ~= &scanFor1; > for ( int i = 0; i < 2; i++ ) { > printf( "scan(%d) = %d\n", i, scan( i ) ); > } > } > > void test2() { > FuncHandler!( OddInt, int ) scan; > scan = new typeof(scan)(); > bit scanFor1( out OddInt rv, int val ) { if ( val == 1 ) { rv = cast(OddInt)10; > return true; } return false; } > scan ~= &scanFor1; > for ( int i = 0; i < 2; i++ ) { > printf( "scan<OddInt>(%d) = %d\n", i, scan( i ) ); > } > } > > int main( char[][] args ) { > test1(); > test2(); > return 0; > } > > // > // bugtest.d(21): function findResult (bit delegate(bit delegate(out > int,int))predi > // cate) does not match argument types (bit delegate(bit delegate(out OddInt > ,int)) > // ) > // > > Mike. > > | |||
Copyright © 1999-2021 by the D Language Foundation
Permalink
Reply