Last active
August 22, 2016 14:23
-
-
Save Cauterite/a21293f7d9ab70b3dbbd164f15f899fd to your computer and use it in GitHub Desktop.
equivalent to reinterpret_cast
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Toº raw_cast(Toº, Fromº)(auto ref Fromº X) @trusted if (Toº.sizeof == Fromº.sizeof) { | |
return *(cast(Toº*) &X); | |
}; | |
unittest { | |
auto foo = raw_cast!int(66.3f); | |
auto baz = raw_cast!(ubyte[size_t.sizeof])(new class {}); | |
static assert(!__traits(compiles, { | |
auto bad = raw_cast!(ubyte[9])(new class {}); | |
})); | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment