Skip to content

Instantly share code, notes, and snippets.

@Cauterite
Last active August 22, 2016 14:23
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Cauterite/a21293f7d9ab70b3dbbd164f15f899fd to your computer and use it in GitHub Desktop.
Save Cauterite/a21293f7d9ab70b3dbbd164f15f899fd to your computer and use it in GitHub Desktop.
equivalent to reinterpret_cast
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