简体   繁体   English

如何使用可变参数模板作为std :: type_index映射的键?

[英]How to use variadic templates as keys to a map of std::type_index?

I'm trying to implement a variadic templated member function that can accept a std::function object with a varying number of template parameters or none at all. 我正在尝试实现一个可变参数的模板化成员函数,该函数可以接受带有可变数量的模板参数或根本没有任何参数的std :: function对象。

Below is a simplified and compilable example of what I am attempting. 下面是我正在尝试的简化且可编译的示例。 I want to be able to dynamically add any user defined data object to my manager. 我希望能够将任何用户定义的数据对象动态添加到我的管理器中。 Then, I want to be able to iterate over any of the data types stored within the manager using variadic templates. 然后,我希望能够使用可变参数模板对管理器中存储的任何数据类型进行迭代。 Where I've commented // HowTo? 我在哪里评论//如何? // is what I'm stuck on. //这就是我所坚持的。 How to I implement my commented out code using variadic templates? 如何使用可变参数模板实现注释掉的代码?

I'm attempting to derive a solution from this post: Possible Solution? 我试图从这篇文章中得出一个解决方案: 可能的解决方案?

#include <functional>
#include <map>
#include <memory>
#include <typeindex>
#include <vector>

constexpr auto Count = 10;

struct Base
{
    virtual ~Base()
    {
    }
};

template <typename T>
struct Container : public Base
{
    std::vector<T> data;
    std::vector<bool> valid;
};

struct Manager
{
    template <typename T>
    void add()
    {
        Container<T>* container{};

        if(this->containers[typeid(T)] == nullptr)
        {
            auto c = std::make_unique<Container<T>>();
            container = c.get();
            this->containers[typeid(T)] = std::move(c);
        }
        else
        {
            container = static_cast<Container<T>*>(this->containers[typeid(T)].get());
        }

        container->data.push_back(T());
        container->valid.push_back(true);
    }

    template <typename ...Args>
    void each(std::function<void(Args&...)> f)
    {
        // HowTo? // auto oneContainer = static_cast<Container<T>*>(this->containers[typeid(DataOne)].get());
        // HowTo? // auto twoContainer = static_cast<Container<T>*>(this->containers[typeid(DataTwo)].get());

        for(auto i = 0; i < Count; i++)
        {
            // HowTo? // if(oneContainer->valid[i] == true && twoContainer->valid[i] == true)
            // HowTo? // f(oneContainer->data[i], twoContainer->data[i]);
        }
    }

    std::map<std::type_index, std::unique_ptr<Base>> containers;
};

struct DataOne
{
    int value{};
};

struct DataTwo
{
    double value{};
};

int main()
{
    Manager manager;

    for(auto i = 0; i < Count; i++)
    {
        manager.add<DataOne>();
        manager.add<DataTwo>();
    }

    manager.each<DataOne>([](DataOne& a) {
        a.value = 0;
    });

    manager.each<DataTwo>([](DataTwo& a) {
        a.value = 0.0;
    });

    manager.each<DataOne, DataTwo>([](DataOne& a, DataTwo& b) {
        a.value = 0;
        b.value = 0.0;
    });
}
auto containers = std::make_tuple<Container<Args>*...>(
  static_cast<Container<Args>*>(this->containers[typeid(Args)].get())...
);

... ...

    for(auto i = 0; i < Count; i++)
    {
      std::apply([&f](auto*... containers){
        bool valid = (containers&&...) && (containers->valid[i]&&...);
        if (valid)
          f( containers->data[i]... );
      }, containers);
    }

Please excuse any typos. 请原谅任何错别字。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM