www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.learn - const ref void[] =?UTF-8?B?4oCTIGZsb2F0W10gbm90IGltcGxpY2l0bHkgYw==?=

reply David <d dav1d.de> writes:
Is this a bug? – http://dpaste.dzfl.pl/31a16eab (code below)


------------
void test(const void* test, size_t length) {
}

void foo_const_ref(const ref void[] data) {
	test(data.ptr, data.length);
}

void foo_ref(ref void[] data) {
	test(data.ptr, data.length);
}

void foo(void[] data) {
	test(data.ptr, data.length);
}


void main() {
	float[] bar = [1.0f, 1.0f, 1.0f, 1.0f, 1.0f];
	void[] v_bar = bar;
	
	foo(bar);
	foo_ref(bar);
	foo_const_ref(bar);
	
	foo(v_bar);
	foo_ref(v_bar);
	foo_const_ref(v_bar);
}
------------
519.d(22): Error: function compileme519.foo_ref (ref void[] data) is not 
callable using argument types (float[])
519.d(22): Error: cast(void[])bar is not an lvalue
519.d(23): Error: function compileme519.foo_const_ref (ref const(void[]) 
data) is not callable using argument types (float[])
519.d(23): Error: cast(const(void[]))bar is not an lvalue
------------

Seems like float[] isn't convertable to void[] anylonger.
Jul 23 2012
next sibling parent David <d dav1d.de> writes:
A workaround:

http://dpaste.dzfl.pl/f424956e

------------
void foo_t(T)(const ref T data) if(isArray!T) {
	test(data.ptr, data.length);
}
------------
Jul 23 2012
prev sibling parent reply "Jonathan M Davis" <jmdavisProg gmx.com> writes:
On Monday, July 23, 2012 12:04:09 David wrote:
 Is this a bug? – http://dpaste.dzfl.pl/31a16eab (code below)
 
 
 ------------
 void test(const void* test, size_t length) {
 }
 
 void foo_const_ref(const ref void[] data) {
 test(data.ptr, data.length);
 }
 
 void foo_ref(ref void[] data) {
 test(data.ptr, data.length);
 }
 
 void foo(void[] data) {
 test(data.ptr, data.length);
 }
 
 
 void main() {
 float[] bar = [1.0f, 1.0f, 1.0f, 1.0f, 1.0f];
 void[] v_bar = bar;
 
 foo(bar);
 foo_ref(bar);
 foo_const_ref(bar);
 
 foo(v_bar);
 foo_ref(v_bar);
 foo_const_ref(v_bar);
 }
 ------------
 519.d(22): Error: function compileme519.foo_ref (ref void[] data) is not
 callable using argument types (float[])

 519.d(22): Error: cast(void[])bar is not an lvalue
 519.d(23): Error: function compileme519.foo_const_ref (ref const(void[])
 data) is not callable using argument types (float[])
 519.d(23): Error: cast(const(void[]))bar is not an lvalue
 ------------
 
 Seems like float[] isn't convertable to void[] anylonger.

Of course that doesn't work. ref must refer to an lvalue, so it can't convert anything. The type must match exactly save for constness. So float[] can't be passed to a function taking ref void[]. Either you need to convert to assign the float[] to a void[] variable first, or you need the function to take ref float[]. - Jonathan M Davis
Jul 23 2012
parent David <d dav1d.de> writes:
 Of course that doesn't work. ref must refer to an lvalue, so it can't convert
 anything. The type must match exactly save for constness. So float[] can't be
 passed to a function taking ref void[]. Either you need to convert to assign
 the float[] to a void[] variable first, or you need the function to take ref
 float[].

 - Jonathan M Davis

Thanks! I guess then I'll stick with the template :)
Jul 23 2012