The following code compiles because I'm assuming that parent of Option<Parent, T, Rest...> itself has no parent.
template <typename Parent, typename T, typename... Rest>
struct Object {
T item; // T is item's type, while Parent is parent's item's type.
Object<T, Rest...>* child; // child's item type is the first type from Rest...
Object<void, Parent, T, Rest...>* parent;
void setChild (Object<T, Rest...>* c) {
child = c;
child->setParent(this);
}
void setParent (Object<void, Parent, T, Rest...>* p) {parent = p;}
};
template <typename Parent, typename T>
struct Object<Parent, T> { // Has no child.
T item;
Object<void, Parent, T>* parent;
void setParent (Object<void, Parent, T>* p) {parent = p;}
};
template <typename... Args>
using ObjectWithNoParent = Object<void, Args...>;
int main() {
ObjectWithNoParent<int, char, double> object;
Object<int, char, double> child;
object.setChild(&child);
Object<char, double> grandChild;
// child.setChild(&grandChild); // Want this line to work.
}
What workaround can I use so that I can replace the type void with a general type?
Aucun commentaire:
Enregistrer un commentaire