映射可变参数模板参数 [英] Map Variadic Template Arguments

查看:67
本文介绍了映射可变参数模板参数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在下面的代码中,我想找到一种更通用的调用GenericPublish__Advertise()的方式,该方式需要一个可变的模板列表.我该怎么做才能改善它?

In the following code, I would like to find a more generic way of calling GenericPublish__Advertise(), which takes a variadic template list. What could I do to improve it?

我想将主题映射到特定类型的发布者:

I would like to map topics to a publisher of a specific type:

  • topic [0]->发布者[0]
  • topic [1]->发布者[1]
  • 如此

虽然代码可以正常工作,但我需要手动编写GenericPublish__Advertise()的模板版本,并手动将主题[i]映射到发布者.我想以某种方式概括GenericPublish__Advertise()的实现.

While the code works ok, I need to manually write the templated versions of GenericPublish__Advertise() and manually map topics[i] to publishers. I would like to somehow generalize the implementation of GenericPublish__Advertise().

非常感谢.

代码:

    #include <iostream>
    #include <memory>
    #include <typeinfo>
    #include <vector>

    class AdvertiseOptionsBase {
    public:
      virtual const std::type_info &GetType() = 0;
    };

    template <typename TSend> 
    class AdvertiseOptions : public AdvertiseOptionsBase {
    public:
      AdvertiseOptions(TSend opt) : opt_(opt) {}
      const std::type_info &GetType() { return typeid(opt_); }

    private:
      TSend opt_;
    };

    class Publisher {
    public:
      Publisher(const std::string &topic) : topic_(topic) {}
      const std::string &GetTopic() const { return topic_; }
      template <typename TSend>
      void SetOptions(const AdvertiseOptions<TSend> &opt) {
        options_ = std::make_unique<AdvertiseOptions<TSend>>(opt);
      }
      const std::unique_ptr<AdvertiseOptionsBase> &GetOptions() const {
        return options_;
      }

    private:
      std::string topic_;
      std::unique_ptr<AdvertiseOptionsBase> options_;
    };

    class Node {
    public:
      template <typename TSend>
      Publisher advertise(std::string topic) {
        Publisher publisher(topic);
        TSend option;
        AdvertiseOptions<TSend> options(option);
        publisher.SetOptions<TSend>(options);
        return publisher;
      }
    };

    template <typename TSend1, typename TSend2>
    void GenericPublish__Advertise(Node &node, std::vector<Publisher> &publishers,
                                   const std::vector<std::string> &topics) {
      publishers.push_back(node.advertise<TSend1>(topics.at(0)));
      publishers.push_back(node.advertise<TSend2>(topics.at(1)));
    }
    template <typename TSend1, typename TSend2, typename TSend3>
    void GenericPublish__Advertise(Node &node, std::vector<Publisher> &publishers,
                                   const std::vector<std::string> &topics) {
      publishers.push_back(node.advertise<TSend1>(topics.at(0)));
      publishers.push_back(node.advertise<TSend2>(topics.at(1)));
      publishers.push_back(node.advertise<TSend3>(topics.at(2)));
    }

    template <typename... TSend>
    class GenericPublish {
    public:
      GenericPublish(const std::vector<std::string> &topics) {
        GenericPublish__Advertise<TSend...>(node_, publishers_, topics);
      }
      void PrintInfo() {
        for (const auto &publisher : publishers_) {
          std::cout << publisher.GetTopic() << " -----> "
                         << (publisher.GetOptions()->GetType()).name() << std::endl;
        }
      }

    protected:
      Node node_;
      std::vector<Publisher> publishers_;

    private:
    };

    int main() {
      std::vector<std::string> topics({"topic_int", "topic_double"});
      GenericPublish<int, double> o(topics);
      o.PrintInfo();
      return 0;
    }

推荐答案

此处的典型方法是使用

The typical approach here is to use the index sequence trick. You take a parameter pack of types, construct an index sequence of the same size, and then iterate over both:

template <typename... TSends> // <== pack of types
void GenericPublishAdvertise(Node &node, std::vector<Publisher> &publishers,
                             const std::vector<std::string> &topics)
{
  GenericPublishAdvertiseImpl<TSends...>(node, publishers, topics,
      std::index_sequence_for<TSends...>()); // <== index sequence creation
}

单独执行:

template <typename... TSends, size_t... Is>
void GenericPublishAdvertiseImpl(Node &node, std::vector<Publisher> &publishers,
    const std::vector<std::string> &topics, std::index_sequence<Is...> )
{
    // since this is C++14
    using swallow = int[];
    (void)swallow{0,
        (void(publishers.push_back(node.advertise<TSends>(topics.at(Is)))), 0)...
        };

    // in C++17, that's just
    (publishers.push_back(node.advertise<TSends>(topics.at(Is))), ...);
}

有关该模式的说明,请参见此答案.

See this answer for explanation of that pattern.

请注意,使用 GenericPublish__Advertise 是UB:保留带双下划线的名称.

Note that using GenericPublish__Advertise is UB: names with double underscores are reserved.

这篇关于映射可变参数模板参数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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