Skip to content

Instantly share code, notes, and snippets.

@aliak00
Created August 3, 2019 18:39
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 aliak00/db6a830d34e458f3cc8bc441bb6b3d6e to your computer and use it in GitHub Desktop.
Save aliak00/db6a830d34e458f3cc8bc441bb6b3d6e to your computer and use it in GitHub Desktop.
checks for copy ctor using __parameters
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