Thread overview
dmd casts but ldc doesn't, and doesn't work in template in dmdm
Jun 05, 2017
Mike B Johnson
Jun 05, 2017
Jonathan M Davis
Jun 05, 2017
Mike B Johnson
Jun 05, 2017
H. S. Teoh
Jun 05, 2017
H. S. Teoh
June 05, 2017
The following line is causing some problems.


    auto bytes = cast(byte[16])guid;


compiles fine in dmd but ldc says it can't convert... also, it doens't work in ctfe/template either. (I'm not sure if ctfe is kicking in or not though, but definitely doesn't work in a template)


June 04, 2017
On Monday, June 05, 2017 00:18:04 Mike B Johnson via Digitalmars-d-learn wrote:
> The following line is causing some problems.
>
>
>      auto bytes = cast(byte[16])guid;
>
>
> compiles fine in dmd but ldc says it can't convert... also, it doens't work in ctfe/template either. (I'm not sure if ctfe is kicking in or not though, but definitely doesn't work in a template)

I assume that guid is a string or array of byte or somesuch? If it's an array of byte or char, then you can probably do

auto bytes = cast(byte[16])guid[0 .. 16];

I doubt that it will work with CTFE though, because this is basically a reinterpret cast, and CTFE gets picky about casts like that. If you need to do something like that in CTFE, you'll probably need to use if(__ctfe) to add a branch that does this it in a for loop or something without any casts.

Also, you probably want ubyte, not byte. byte is signed, so it really only makes sense to use it as an integral value that holds [-128, 128] rather than for an actual byte value.

- Jonathan M Davis

June 04, 2017
On Mon, Jun 05, 2017 at 01:14:31AM +0000, Mike B Johnson via Digitalmars-d-learn wrote: [...]
> Guid is a struct and I am trying to get the "bytes" of the struct" to get the guid bytes. It is quicker than accessing all the elements one at a time.

	union U {
		typeof(guid) guid;
		ubyte[guid.sizeof] bytes;
	}
	U u;
	u.guid = guid;
	// ... do something with u.bytes.


T

-- 
Why are you blatanly misspelling "blatant"? -- Branden Robinson
June 05, 2017
On Monday, 5 June 2017 at 00:56:52 UTC, Jonathan M Davis wrote:
> On Monday, June 05, 2017 00:18:04 Mike B Johnson via Digitalmars-d-learn wrote:
>> [...]
>
> I assume that guid is a string or array of byte or somesuch? If it's an array of byte or char, then you can probably do
>
> auto bytes = cast(byte[16])guid[0 .. 16];
>
> I doubt that it will work with CTFE though, because this is basically a reinterpret cast, and CTFE gets picky about casts like that. If you need to do something like that in CTFE, you'll probably need to use if(__ctfe) to add a branch that does this it in a for loop or something without any casts.
>
> Also, you probably want ubyte, not byte. byte is signed, so it really only makes sense to use it as an integral value that holds [-128, 128] rather than for an actual byte value.
>
> - Jonathan M Davis

Guid is a struct and I am trying to get the "bytes" of the struct" to get the guid bytes. It is quicker than accessing all the elements one at a time.
June 04, 2017
On Sun, Jun 04, 2017 at 06:12:42PM -0700, H. S. Teoh wrote:
> On Mon, Jun 05, 2017 at 01:14:31AM +0000, Mike B Johnson via Digitalmars-d-learn wrote: [...]
> > Guid is a struct and I am trying to get the "bytes" of the struct" to get the guid bytes. It is quicker than accessing all the elements one at a time.
> 
> 	union U {
> 		typeof(guid) guid;
> 		ubyte[guid.sizeof] bytes;
> 	}
> 	U u;
> 	u.guid = guid;
> 	// ... do something with u.bytes.
[...]

And if you're going to be doing this a lot on many different types, you could ease the typing by declaring a template for it, for example:

	union AsBytes(T) {
		T t;
		ubyte[T.sizeof] bytes;
	}
	ubyte[T.sizeof] asBytes(T)(T t) {
		AsBytes!T u;
		u.t = t;
		return u.bytes;
	}
	...
	struct S {
		/* stuff */
	}

	S s;
	auto bytes = s.asBytes;
	... /* do stuff with bytes, which is a static array of ubyte */

Note, of course, that this will be @system if T contains any pointers.


T

-- 
2+2=4. 2*2=4. 2^2=4. Therefore, +, *, and ^ are the same operation.