www.digitalmars.com         C & C++   DMDScript  

digitalmars.D - Unions and CTFE

reply Gianni Pisetta <pisetta.gianni alice.it> writes:
Hi all,
i'm coding a parametrized crc implementation that can support 
most of the standards. I want to make it work with CTFE and i 
stumbled upon a difficulty when using 
std.bitmanip.nativeToLittleEndian and 
std.bitmanip.nativeToBigEndian.
The code below is the concept used by EndianSwapper and it does 
not compile because of unions and CTFE. Also as the error message 
is very obscure, is it intended behaviour or a bug?

module testunionctfe;

import std.traits;

auto f(T)(T data) {
   union DataBytes {
     Unqual!T data;
     ubyte[T.sizeof] bytes;
   };

   DataBytes tmp;
   tmp.data = data;
   return tmp.bytes;
}

void main(){
   auto a = f( 0x01020304 );
   enum b = f( 0x01020304 ); //testunionctfe.d(18): Error: 
uninitialized variable 'data' cannot be returned from CTFE
   assert( a == b );
}

If it is intended behaviour, i think i come up with an 
alternative implementation for these two functions with shifts 
and masks to be used only in ctfe(union implementation is faster) 
when the type is uint and ushort(and maybe ulong), that i will 
prepare in a pull request.
Otherwise if it is a bug i will file a bugreport instead.
Dec 18 2015
parent reply Jimmy Cao <jc2462 cornell.edu> writes:
On Friday, 18 December 2015 at 14:30:25 UTC, Gianni Pisetta wrote:
 Hi all,
 i'm coding a parametrized crc implementation that can support 
 most of the standards. I want to make it work with CTFE and i 
 stumbled upon a difficulty when using 
 std.bitmanip.nativeToLittleEndian and 
 std.bitmanip.nativeToBigEndian.
 The code below is the concept used by EndianSwapper and it does 
 not compile because of unions and CTFE. Also as the error 
 message is very obscure, is it intended behaviour or a bug?
I think it's intended behavior. According to http://dlang.org/changelog/2.065.0.html#ctfe-overlapped-field "Bit image reinterpretation by using two overlapped union fields is not allowed during CTFE."
Dec 18 2015
parent Gianni Pisetta <pisetta.gianni alice.it> writes:
On Friday, 18 December 2015 at 17:21:39 UTC, Jimmy Cao wrote:
 On Friday, 18 December 2015 at 14:30:25 UTC, Gianni Pisetta 
 wrote:
 Hi all,
 i'm coding a parametrized crc implementation that can support 
 most of the standards. I want to make it work with CTFE and i 
 stumbled upon a difficulty when using 
 std.bitmanip.nativeToLittleEndian and 
 std.bitmanip.nativeToBigEndian.
 The code below is the concept used by EndianSwapper and it 
 does not compile because of unions and CTFE. Also as the error 
 message is very obscure, is it intended behaviour or a bug?
I think it's intended behavior. According to http://dlang.org/changelog/2.065.0.html#ctfe-overlapped-field "Bit image reinterpretation by using two overlapped union fields is not allowed during CTFE."
Yeah, i supposed it. But the error message was misleading, because 'data' is initialized and 'bytes' is the actual uninitialized part. I guess the error message can be improved. Anyway thanks
Dec 18 2015