Possible Bug with Interfaces in Variadic Functions?
Daniel Giddings
danielg at microforte.com.au
Sun Sep 17 18:10:57 PDT 2006
Hi All, I'm new to D, and have come across the following problem. I
thought it best to ask about it before reporting it as a bug.
The problem I've come across is passing a class with an interface base
into a variadic function and accessing it as the interface. It crashes
the program with some strange behaviour. As far as I can tell I'm not
doing anything wrong.
Anyway, here's the program:
--------------------------------------------------------
import std.stdio;
import std.stdarg;
class B
{
char[] f() { return "B.f"; }
}
class DB : B
{
char[] f() { return "DB.f"; }
}
interface I
{
char[] f();
}
class CI : I
{
char[] f() { return "CI.f"; }
}
void output( ... )
{
for( int i = 0; i < _arguments.length; ++i )
{
if( _arguments[i] == typeid(int) )
writefln( "int: %s", va_arg!(int)(_argptr) );
if( _arguments[i] == typeid(char[]) )
writefln( "char[]: %s", va_arg!(char[])(_argptr) );
if( _arguments[i] == typeid(B) )
writefln( "B: %s", va_arg!(B)(_argptr).f() );
if( _arguments[i] == typeid(I) )
{
writefln( "Print here for test purposes only - CI appears after this" );
writefln( "I: %s", va_arg!(I)(_argptr).f() );
}
}
}
void main()
{
output( 5, "Hello World!", new DB, new CI );
}
--------------------------------------------------------
The output I'm receiving is:
int: 5
char[]: Hello World!
B: DB.f
Print here for test purposes only - CI appears after this
CI
I: Error: Access Violation
where as I would have expected:
int: 5
char[]: Hello World!
B: DB.f
Print here for test purposes only - CI appears after this
I: CI.f
I added the class example with a base class as well, and it behaves as I
expected.
Is there anything I'm doing incorrectly? It's easy enough to get around
the problem for what I want to do, but I thought I'd bring it up.
Cheers,
:-) Dan
More information about the Digitalmars-d-learn
mailing list