I'm trying to use a variadic templated class to make a structure of parent-child relationships.
#include <tuple>
template<typename TParentTuple, typename TChilderenTuple>
class Obj;
template<typename... TParents, typename... TChildren>
class Obj<std::tuple<TParents...>, std::tuple<TChildren...>> {
private:
std::tuple<std::vector<std::shared_ptr<TParents...>>> parentsVectors;
std::tuple<std::vector<std::shared_ptr<TChildren...>>> childrenVectors;
};
using Tree = Obj<std::tuple<>, std::tuple<>>;
using Dog = Obj<std::tuple<>, std::tuple<>>;
using Parasite = Obj<std::tuple<>, std::tuple<>>;
using Human = Obj<std::tuple<>,std::tuple<Tree, Dog>>;
using Tree = Obj<std::tuple<Human>, std::tuple<>>;
using Dog = Obj<std::tuple<Human>, std::tuple<Parasite>>;
using Parasite = Obj<std::tuple<Dog>, std::tuple<>>;
int main() {}
This gives me this kind of error in MSVS2017:
error C2371: 'Tree': redefinition; different basic types
And this in GCC:
error: conflicting declaration
I've been looking all over the web on how to fix this, but haven't found it yet. Is this fixable?
What I really am trying to do is to use metaprogramming to prevent me to write a lot of duplicate code, as I did in this answer
Ok, let's expand. The relationship is explained in this question
I've been writing the Human
Dog
, etc classes like
Human.hpp
#include "BaseObject.hpp"
#include "Tree.hpp"
#include "Dog.hpp"
class Tree;
class Dog;
class Human : public BaseObject {
public:
prtVector<BaseObject> getAllParents() const override;
prtVector<BaseObject> getAllChildren() const override;
void removeAllParents() override;
void removeAllChildren() override ;
friend class Dog;
friend class Tree;
template<class A, class B>
friend void addRelation(A* a, B* b);
private:
void addParent(Human* const);
void removeParent(Human const* const);
void addChild(Human* const);
void removeChild(Human const* const);
void addChild(Tree* const);
void removeChild(Tree const* const);
void addChild(Dog* const);
void removeChild(Dog const* const);
private:
prtVector<Human> parents;
prtVector<Human> children;
prtVector<Tree> plants;
prtVector<Dog> pets;
};
Human.cpp
#include "Human.hpp"
prtVector<BaseObject> Human::getAllParents() const {
prtVector<BaseObject> result(std::cbegin(parents), std::cend(parents));
return result;
}
prtVector<BaseObject> Human::getAllChildren() const {
prtVector<BaseObject> result(std::cbegin(children), std::cend(children));
result.insert(std::end(result), std::cbegin(pets), std::cend(pets));
result.insert(std::end(result), std::cbegin(plants), std::cend(plants));
return result;
}
void Human::removeAllParents() {
for (auto parent : parents) { parent->removeChild(this); }
parents.clear();
}
void Human::removeAllChildren() {
for (auto child : children) { child->removeParent(this); } children.clear();
for (auto pet : pets) { pet->removeParent(this); } pets.clear();
for (auto plant : plants) { plant->removeParent(this); } plants.clear();
}
void Human::addParent(Human* const parent) { parents.push_back(parent); }
#include <algorithm>
void Human::removeParent(Human const* const parent) {
auto it = std::find(std::cbegin(parents), std::cend(parents), parent);
if (it != std::cend(parents)) parents.erase(it);
}
void Human::addChild(Human* const child) { children.push_back(child); }
void Human::removeChild(Human const* const child) {
auto it = std::find(std::cbegin(children), std::cend(children), child);
if (it != std::cend(children)) children.erase(it);
}
void Human::addChild(Dog* const pet) { pets.push_back(pet); }
void Human::removeChild(Dog const* const pet) {
auto it = std::find(std::cbegin(pets), std::cend(pets), pet);
if (it != std::cend(pets)) pets.erase(it);
}
void Human::addChild(Tree* const plant) { plants.push_back(plant); }
void Human::removeChild(Tree const* const plant) {
auto it = std::find(std::cbegin(plants), std::cend(plants), plant);
if (it != std::cend(plants)) plants.erase(it);
}
Same for Dog
:Dog.hpp
#include "BaseObject.hpp"
#include "Human.hpp"
#include "Parasite.hpp"
class Human;
class Parasite;
class Dog : public BaseObject {
public:
prtVector<BaseObject> getAllParents() const override;
prtVector<BaseObject> getAllChildren() const override;
void removeAllParents() override;
void removeAllChildren() override;
friend class Human;
friend class Parasite;
template<class A, class B>
friend void addRelation(A* a, B* b);
private:
void addParent(Human* const);
void removeParent(Human const* const);
void addChild(Parasite* const);
void removeChild(Parasite const* const);
private:
prtVector<Human> owners;
prtVector<Parasite> parasites;
};
Dog.cpp
#include "Dog.hpp"
prtVector<BaseObject> Dog::getAllParents() const {
prtVector<BaseObject> result(std::cbegin(owners), std::cend(owners));
return result;
}
prtVector<BaseObject> Dog::getAllChildren() const {
prtVector<BaseObject> result(std::cbegin(parasites), std::cend(parasites));
return result;
}
void Dog::removeAllParents() {
for (auto owner : owners) { owner->removeChild(this); }
owners.clear();
}
void Dog::removeAllChildren() {
for (auto parasite : parasites) { parasite->removeParent(this); }
parasites.clear();
}
void Dog::addParent(Human* const owner) { owners.push_back(owner); }
#include <algorithm>
void Dog::removeParent(Human const* const owner) {
auto it = std::find(std::cbegin(owners), std::cend(owners), owner);
if (it != std::cend(owners)) owners.erase(it);
}
void Dog::addChild(Parasite* const parasite) { parasites.push_back(parasite); }
void Dog::removeChild(Parasite const* const parasite) {
auto it = std::find(std::cbegin(parasites), std::cend(parasites), parasite);
if (it != std::cend(parasites)) parasites.erase(it);
}
And so on for Tree
and Parasite
. As you can see, there is a lot of duplicate code in there! I was thinking that metaprogramming should be able to help me there: generating functions for specific types passed as variadic template arguments.
I think I see what you are trying to accomplish, and I believe you can get there using tag types:
template<typename TParentTuple, typename TChilderenTuple>
class Obj;
template<typename TParents, typename TChildren>
class Obj<std::tuple<TParents>, std::tuple<TChildren>> {};
struct Human_tag {};
struct Tree_tag {};
struct Dog_tag {};
struct Parasite_tag {};
using Human = Obj<std::tuple<>,std::tuple<Tree_tag, Dog_tag>>;
using Tree = Obj<std::tuple<Human_tag>, std::tuple<>>;
using Dog = Obj<std::tuple<Human_tag>, std::tuple<Parasite_tag>>;
using Parasite = Obj<std::tuple<Dog_tag>, std::tuple<>>;
You'll most likely hit some other recursive mess down the line though.
Edit: expanding the code to show how to map from the tag back to the types:
#include <tuple>
#include <vector>
template<typename TParentTuple, typename TChilderenTuple>
class Obj;
template<typename... TParents, typename... TChildren>
class Obj<std::tuple<TParents...>, std::tuple<TChildren...>> {
public:
std::tuple<std::vector<typename TParents::obj_type*>...> parents_;
std::tuple<std::vector<typename TChildren::obj_type*>...> children_;
};
struct Human_tag;
struct Tree_tag;
struct Dog_tag;
struct Parasite_tag;
using Human = Obj<std::tuple<>,std::tuple<Tree_tag, Dog_tag>>;
using Tree = Obj<std::tuple<Human_tag>, std::tuple<>>;
using Dog = Obj<std::tuple<Human_tag>, std::tuple<Parasite_tag>>;
using Parasite = Obj<std::tuple<Dog_tag>, std::tuple<>>;
struct Human_tag {
using obj_type = Human;
};
struct Tree_tag {
using obj_type = Tree;
};
struct Dog_tag {
using obj_type = Dog;
};
struct Parasite_tag {
using obj_type = Parasite;
};
void foo() {
Tree t;
Human h;
std::get<0>(t.parents_).emplace_back(&h);
}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.