冲突声明/重新定义:不同的基本类型 [英] Conflictions declaration / redefinition: different basic types

查看:69
本文介绍了冲突声明/重新定义:不同的基本类型的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用可变参数模板类来构建父子关系的结构.

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() {}

这在 MSVS2017 中给了我这种错误:

This gives me this kind of error in MSVS2017:

错误 C2371:树":重新定义;不同的基本类型

error C2371: 'Tree': redefinition; different basic types

这在 GCC 中:

错误:声明冲突

我一直在网上寻找如何解决这个问题,但还没有找到.这个可以解决吗?

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

好的,让我们展开.这个问题

我一直在写 Human Dog 等类

人类.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);
}

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;
};

狗.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);
}

对于TreeParasite 依此类推.如您所见,那里有很多重复的代码!我认为元编程应该能够帮助我:为作为可变参数模板参数传递的特定类型生成函数.

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.

展开代码以显示如何从标签映射回类型:

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);
}

这篇关于冲突声明/重新定义:不同的基本类型的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆