Created
August 3, 2019 18:39
-
-
Save aliak00/db6a830d34e458f3cc8bc441bb6b3d6e to your computer and use it in GitHub Desktop.
checks for copy ctor using __parameters
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
template hasNonTrivialCopyConstructor(T) { | |
bool isCopyConstructorFor(alias ctor, T)() { | |
// Specialized function that checks specifically for "ref" in a string | |
bool containsRef(string str) { | |
foreach (i, c; str) { | |
if (c == 'r') { | |
if (i + 2 < str.length) { | |
return str[i + 1] == 'e' && str[i + 2] == 'f'; | |
} | |
} | |
} | |
return false; | |
} | |
static if (is(typeof(ctor) Params == __parameters) && Params.length >= 1) { | |
// Check that Params 0 is correct type for a copy constructro | |
enum isCorrectType = is(Params[0] == T); | |
// If more than one parameter, check that they are all defaulted | |
static if (Params.length >= 1) { | |
bool hasCorrectDefaults() { | |
bool result = true; | |
static foreach (I; 1 .. Params.length) {{ | |
auto getDefault(Params[I..I+1] args) { return args[0]; } | |
result &= is(typeof(getDefault())); | |
}} | |
return result; | |
} | |
} else { | |
enum hasCorrectDefaults = true; | |
} | |
return isCorrectType | |
&& containsRef(Params[0..1].stringof) | |
&& hasCorrectDefaults; | |
} else { | |
return false; | |
} | |
} | |
bool impl() { | |
static if (__traits(hasMember, T, "__ctor")) { | |
foreach (f; __traits(getOverloads, T, "__ctor")) { | |
auto yes = isCopyConstructorFor!(f, T); | |
if (yes) { | |
return yes; | |
} | |
} | |
} | |
return false; | |
} | |
enum hasNonTrivialCopyConstructor = impl; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment