www.digitalmars.com         C & C++   DMDScript  

D - Templates, interfaces, access violation problem

reply =?ISO-8859-1?Q?Sigbj=F8rn_Lund_Olsen?= <sigbjorn lundolsen.net> writes:
Having a little problem here with the code below. It compiles all right, 
but when run it gives me the descriptive error message of "Error: Access 
Violation". What am I doing wrong?

template StreamInterfaces(T)
{
	interface ReadableStreamInterface
	{
		void read(out T[] buffer, in uint number);
		void sync();
	}
}

template MemoryStream(T)
{
	class MemoryStream : StreamInterfaces!(T).ReadableStreamInterface
	{
		void read(out T[] buffer, in uint number)
		{
			printf("read\n");
		}

		void sync()
		{
			printf("sync\n");
		}
	}
}

int main()
{
	alias MemoryStream!(ubyte) MemoryUByteStream;
	MemoryUByteStream testStream;
	testStream.sync();
	
	ubyte[] buffer;
	testStream.read(buffer, 100);
	
	return 0;
}

Cheers,
Sigbjørn Lund Olsen
Mar 21 2004
parent reply =?ISO-8859-1?Q?Sigbj=F8rn_Lund_Olsen?= <sigbjorn lundolsen.net> writes:
Fllrb! Obviously, I am still living the the C++ dreamscape where an 
object is automatically constructed when declared. Adding "= new 
MemoryUByteStream" fixed it.

Is there any reason why one has to explicitly 'new' an object?

Cheers,
Sigbjørn Lund Olsen
Mar 21 2004
parent reply J Anderson <REMOVEanderson badmama.com.au> writes:
Sigbjørn Lund Olsen wrote:

 Fllrb! Obviously, I am still living the the C++ dreamscape where an 
 object is automatically constructed when declared. Adding "= new 
 MemoryUByteStream" fixed it.

 Is there any reason why one has to explicitly 'new' an object? 
Yes and no. Objects in D are pointers to objects not objects themselves, therefore its pretty close to C++ (because u have to use new there as well, except D is one symbol shorter). Whenever u use new, think gc. I think it may be easier to parse as well. Of course the syntax could have been simplified (and this has been discussed before). If you really want the C++ behaviour then it would be the auto version of object creation that you'd want to simplify. auto object o = new object; to auto object o; That was discussed before. -- -Anderson: http://badmama.com.au/~anderson/
Mar 21 2004
next sibling parent "Matthew" <matthew stlsoft.org> writes:
"J Anderson" <REMOVEanderson badmama.com.au> wrote in message
news:c3jpf3$rus$1 digitaldaemon.com...
 Sigbjørn Lund Olsen wrote:

 Fllrb! Obviously, I am still living the the C++ dreamscape where an
 object is automatically constructed when declared. Adding "= new
 MemoryUByteStream" fixed it.

 Is there any reason why one has to explicitly 'new' an object?
Yes and no. Objects in D are pointers to objects not objects themselves, therefore its pretty close to C++ (because u have to use new there as well, except D is one symbol shorter). Whenever u use new, think gc. I think it may be easier to parse as well. Of course the syntax could have been simplified (and this has been discussed before). If you really want the C++ behaviour then it would be the auto version of object creation that you'd want to simplify. auto object o = new object; to auto object o;
This should be done. The auto qualifier prevents it from being ambiguous (to compiler or human) and it removes the quite confusing new'ing of an object that's automatic.
Mar 21 2004
prev sibling parent reply =?ISO-8859-1?Q?Sigbj=F8rn_Lund_Olsen?= <sigbjorn lundolsen.net> writes:
J Anderson wrote:

 Sigbjørn Lund Olsen wrote:
 
 Fllrb! Obviously, I am still living the the C++ dreamscape where an 
 object is automatically constructed when declared. Adding "= new 
 MemoryUByteStream" fixed it.

 Is there any reason why one has to explicitly 'new' an object? 
Yes and no. Objects in D are pointers to objects not objects themselves, therefore its pretty close to C++ (because u have to use new there as well, except D is one symbol shorter). Whenever u use new, think gc. I think it may be easier to parse as well. Of course the syntax could have been simplified (and this has been discussed before). If you really want the C++ behaviour then it would be the auto version of object creation that you'd want to simplify. auto object o = new object; to auto object o; That was discussed before.
Mmhm. Remember what the thread was named (just out of curiousity)? I'm not too fussed about wanting C++ behaviour just for the sake of it - there are too many who'd like D to me an amalgamation of languages Foo, explicitly writing 'new object' isn't going to be a problem, for me at least, as long as I remember to do it. My principal worry is that I'm going to make hordes of bugs while my mind adjusts (and afterwards too?). Error messages, really. I had no idea what was going on for quite a while there, or where it was. Presumably it wouldn't be hard for the compiler to see that 'oh, here's a fool trying to do something to an object that hasn't been constructed - let's throw a compilation error at her' instead of leaving it untill runtime, with as giving a message as "Access Violation, and I'm not going to give you fools a line number, module name, function name, nothing to help you along. So long, and see you next time." (paraphrase) Cheers, Sigbjørn Lund Olsen
Mar 21 2004
next sibling parent "Matthew" <matthew stlsoft.org> writes:
"Sigbjørn Lund Olsen" <sigbjorn lundolsen.net> wrote in message
news:c3jqj9$ti2$1 digitaldaemon.com...
 J Anderson wrote:

 Sigbjørn Lund Olsen wrote:

 Fllrb! Obviously, I am still living the the C++ dreamscape where an
 object is automatically constructed when declared. Adding "= new
 MemoryUByteStream" fixed it.

 Is there any reason why one has to explicitly 'new' an object?
Yes and no. Objects in D are pointers to objects not objects themselves, therefore its pretty close to C++ (because u have to use new there as well, except D is one symbol shorter). Whenever u use new, think gc. I think it may be easier to parse as well. Of course the syntax could have been simplified (and this has been discussed before). If you really want the C++ behaviour then it would be the auto version of object creation that you'd want to simplify. auto object o = new object; to auto object o; That was discussed before.
Mmhm. Remember what the thread was named (just out of curiousity)? I'm not too fussed about wanting C++ behaviour just for the sake of it - there are too many who'd like D to me an amalgamation of languages Foo, explicitly writing 'new object' isn't going to be a problem, for me at least, as long as I remember to do it.
It's appropriate to have to write new when you're constructing a heap based object, but it's a dose of cognitive dissonance to have to do it when the object is auto. We don't have to new structs, do we?
 My principal worry is that I'm going to make hordes of bugs while my
 mind adjusts (and afterwards too?). Error messages, really. I had no
 idea what was going on for quite a while there, or where it was.
 Presumably it wouldn't be hard for the compiler to see that 'oh, here's
 a fool trying to do something to an object that hasn't been constructed
 - let's throw a compilation error at her' instead of leaving it untill
 runtime, with as giving a message as "Access Violation, and I'm not
 going to give you fools a line number, module name, function name,
 nothing to help you along. So long, and see you next time." (paraphrase)
The assertion messages are Sh, and need addressing asap. I think there's not much debate on that point, and hopefully Walter'll deal with that just as soon as he's done the whole list of new libraries I just sent him. ;-)
Mar 21 2004
prev sibling parent reply "Phill" <phill pacific.net.au> writes:
"Sigbjørn Lund Olsen" <sigbjorn lundolsen.net> wrote in message
news:c3jqj9$ti2$1 digitaldaemon.com...
 J Anderson wrote:

 Sigbjørn Lund Olsen wrote:

 Fllrb! Obviously, I am still living the the C++ dreamscape where an
 object is automatically constructed when declared. Adding "= new
 MemoryUByteStream" fixed it.

 Is there any reason why one has to explicitly 'new' an object?
Yes and no. Objects in D are pointers to objects not objects themselves, therefore its pretty close to C++ (because u have to use new there as well, except D is one symbol shorter). Whenever u use new, think gc. I think it may be easier to parse as well. Of course the syntax could have been simplified (and this has been discussed before). If you really want the C++ behaviour then it would be the auto version of object creation that you'd want to simplify. auto object o = new object; to auto object o; That was discussed before.
Mmhm. Remember what the thread was named (just out of curiousity)? I'm not too fussed about wanting C++ behaviour just for the sake of it - there are too many who'd like D to me an amalgamation of languages Foo, explicitly writing 'new object' isn't going to be a problem, for me at least, as long as I remember to do it. My principal worry is that I'm going to make hordes of bugs while my mind adjusts (and afterwards too?). Error messages, really. I had no idea what was going on for quite a while there, or where it was. Presumably it wouldn't be hard for the compiler to see that 'oh, here's a fool trying to do something to an object that hasn't been constructed - let's throw a compilation error at her'
Does this mean that you are the first DWoman on the NG? Phill.
Mar 22 2004
parent reply =?ISO-8859-1?Q?Sigbj=F8rn_Lund_Olsen?= <sigbjorn lundolsen.net> writes:
Phill wrote:

 "Sigbjørn Lund Olsen" <sigbjorn lundolsen.net> wrote in message
 news:c3jqj9$ti2$1 digitaldaemon.com...
 
J Anderson wrote:


Sigbjørn Lund Olsen wrote:


Fllrb! Obviously, I am still living the the C++ dreamscape where an
object is automatically constructed when declared. Adding "= new
MemoryUByteStream" fixed it.

Is there any reason why one has to explicitly 'new' an object?
Yes and no. Objects in D are pointers to objects not objects themselves, therefore its pretty close to C++ (because u have to use new there as well, except D is one symbol shorter). Whenever u use new, think gc. I think it may be easier to parse as well. Of course the syntax could have been simplified (and this has been discussed before). If you really want the C++ behaviour then it would be the auto version of object creation that you'd want to simplify. auto object o = new object; to auto object o; That was discussed before.
Mmhm. Remember what the thread was named (just out of curiousity)? I'm not too fussed about wanting C++ behaviour just for the sake of it - there are too many who'd like D to me an amalgamation of languages Foo, explicitly writing 'new object' isn't going to be a problem, for me at least, as long as I remember to do it. My principal worry is that I'm going to make hordes of bugs while my mind adjusts (and afterwards too?). Error messages, really. I had no idea what was going on for quite a while there, or where it was. Presumably it wouldn't be hard for the compiler to see that 'oh, here's a fool trying to do something to an object that hasn't been constructed - let's throw a compilation error at her'
Does this mean that you are the first DWoman on the NG?
No, it means I like writing 'she' where everyone else (it seems) likes writing 'him', because it catches people off guard and tends to make them think a little bit. Which can't possibly be a bad thing :-) Cheers, Sigbjørn Lund Olsen
Mar 22 2004
parent "Phill" <phill pacific.net.au> writes:
fair enough :o))


"Sigbjørn Lund Olsen" <sigbjorn lundolsen.net> wrote in message
news:c3m9ct$1rn5$1 digitaldaemon.com...
 Phill wrote:

 "Sigbjørn Lund Olsen" <sigbjorn lundolsen.net> wrote in message
 news:c3jqj9$ti2$1 digitaldaemon.com...

J Anderson wrote:


Sigbjørn Lund Olsen wrote:


Fllrb! Obviously, I am still living the the C++ dreamscape where an
object is automatically constructed when declared. Adding "= new
MemoryUByteStream" fixed it.

Is there any reason why one has to explicitly 'new' an object?
Yes and no. Objects in D are pointers to objects not objects themselves, therefore its pretty close to C++ (because u have to use new there as well,
except
D is one symbol shorter).   Whenever u use new, think gc.  I think it
may be easier to parse as well.

Of course the syntax could have been simplified (and this has been
discussed before).  If you really want the C++ behaviour then it would
be the auto version of object creation that you'd want to simplify.

auto object o = new object;

to

auto object o;

That was discussed before.
Mmhm. Remember what the thread was named (just out of curiousity)? I'm not too fussed about wanting C++ behaviour just for the sake of it - there are too many who'd like D to me an amalgamation of languages Foo, explicitly writing 'new object' isn't going to be a problem, for me at least, as long as I remember to do it. My principal worry is that I'm going to make hordes of bugs while my mind adjusts (and afterwards too?). Error messages, really. I had no idea what was going on for quite a while there, or where it was. Presumably it wouldn't be hard for the compiler to see that 'oh, here's a fool trying to do something to an object that hasn't been constructed - let's throw a compilation error at her'
Does this mean that you are the first DWoman on the NG?
No, it means I like writing 'she' where everyone else (it seems) likes writing 'him', because it catches people off guard and tends to make them think a little bit. Which can't possibly be a bad thing :-) Cheers, Sigbjørn Lund Olsen
Mar 22 2004