Created
August 12, 2010 15:14
-
-
Save sinfu/521127 to your computer and use it in GitHub Desktop.
構造体のconstをtailconstに弱める
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
struct S | |
{ | |
int[] array; | |
int value; | |
R another; | |
} | |
struct R | |
{ | |
int[] array; | |
} | |
void main() | |
{ | |
ReduceConst!(const S) rcs; | |
/* | |
* struct RCS | |
* { | |
* const(int)[] array; | |
* int value | |
* RCR another; | |
* } | |
* struct RCR | |
* { | |
* const(int)[] array; | |
* } | |
*/ | |
static assert(is(typeof(rcs.array) == const(int)[])); | |
static assert(is(typeof(rcs.value) == int )); | |
auto rca = rcs.another; | |
static assert(is(typeof(rca.array) == const(int)[])); | |
} | |
//----------------------------------------------------------------------------// | |
template ReduceConst(T) | |
if (!is(T == const)) | |
{ | |
alias T ReduceConst; | |
} | |
template ReduceConst(T) | |
if (is(T == const) && !is(T == struct)) | |
{ | |
static if (is(T R == const R)) | |
alias R ReduceConst; | |
else | |
static assert(0); | |
} | |
struct ReduceConst(S) | |
if (is(S == const) && is(S == struct)) | |
{ | |
mixin (ReduceConst_inject!S); | |
} | |
private template ReduceConst_inject(S) | |
if (is(S == struct)) | |
{ | |
static assert(is(S == const)); | |
enum string ReduceConst_inject = | |
{ | |
string fields; | |
foreach (member; __traits(allMembers, S)) | |
{ | |
static if (is(MemberType!(S, member) M) && !is(M == function)) | |
{ | |
auto type = `MemberType!(S, "`~ member ~`")`; | |
fields ~= "ReduceConst!(const "~ type ~") "~ member ~";"; | |
} | |
} | |
return fields; | |
}(); | |
} | |
//----------------------------------------------------------------------------// | |
template MemberType(A, string member) | |
{ | |
alias typeof(__traits(getMember, A.init, member)) MemberType; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment