简体   繁体   English

c++11将std::tuple解包成虚成员函数

[英]c++11 unpack std::tuple into virtual member function

The full story:完整的故事:

I'm trying to build a framework that looks a bit like this:我正在尝试构建一个看起来有点像这样的框架:

#include <tuple>
#include <memory>
using namespace std;

// this class allows user to call "run" without any args 
class simulation_base{
public:
    int run(){ execute_simulation_wrapped(); }; 
protected:
    virtual int execute_simulation_wrapped(); {return 0;};
}

// this class funnels some stored inputs into a soon-to-be-overridden method
template <typename Ts...>
class simulation_wrapper : public simulation_base {
    tuple<shared_ptr<Ts>... > stored_inputs;

    public:
    int execute_simulation_wrapped() {/* how do you call simulation method? */};

    protected:
    virtual int simulation(const Ts&...){return 0};
}

Now we can use the framework to define a couple of simple-looking classes that can be simulated..现在我们可以使用框架来定义几个可以模拟的简单的类。

class jones_household : public simulation_wrapper< woman, girl, cat >{
    int simulation(woman mrs_jones, girl mary, cat sniffles)
         // mrs_jones and her daugther mary play with sniffles the cat
         return 1;
    }
}

class smith_household : public simulation_wrapper< man, dog >{
    int simulation(man mr_smith, dog fido)
         // mr_smith and his dog fido go for a walk
         return 1;
    }
}

And then build a multiverse of these simulatable households...然后建立这些可模拟家庭的多元宇宙......

smith_household uinverse_1_smiths;
smith_household uinverse_2_smiths;
jones_houshold uinverse_1_jones;
jones_houshold uinverse_2_jones;

// set the values of the stored_inputs (i.e. fido, sniffles etc.) 

Finally, we get to the point: we want to be able to write a function which is agnostic of household type, but is still able to call run on the simulation:最后,我们进入正题:我们希望能够编写一个与家庭类型无关的函数,但仍然能够在模拟上调用run

void play_simulation(simulation_base& some_household){
     // do some general stuff...

     some_household.run();  
}

In summary: run calls the relevant templated-instance of the virtual method execute_simulation_wrapped , which then unpacks the stored_inputs and provides them to the virtual simulation function which has a customized implementation for each household.总之: run调用虚拟方法execute_simulation_wrapped的相关模板化实例,然后解包存储的stored_inputs并将它们提供给虚拟simulation函数,该函数为每个家庭定制了实现。


The question that I think I should be asking:我认为我应该问的问题:

So, I think I've got most of the above set up right, but I've been looking at this for a long time and I still cant work out how the simulation_wrapper::execute_simulation_wrapped function can make the call to simulation and provide the unpacked tuple stored_inputs as a parameter pack.所以,我认为我已经正确设置了上述大部分内容,但是我已经研究了很长时间,但我仍然无法弄清楚simulation_wrapper::execute_simulation_wrapped函数如何调用simulation并提供解压元组stored_inputs作为参数包。

I know there are SO questions and blogs out there that give details as to how to call a regular function with an unpacked tuple, but I haven't managed to extend this to member functions, and specifically virtual member functions.我知道有很多问题和博客提供了有关如何使用解包元组调用常规函数的详细信息,但我还没有设法将其扩展到成员函数,特别是虚拟成员函数。

TMP is new to me and still thoroughly confusing, so fairly explicit answers would be much appreciated! TMP 对我来说是新的并且仍然非常混乱,所以非常明确的答案将不胜感激!

This is usually done with a help of index_sequence :这通常是在index_sequence的帮助下index_sequence

template <typename... Ts>
class simulation_wrapper : public simulation_base
{
    tuple<shared_ptr<Ts>... > stored_inputs{new Ts...};

public:
// MAGIC STARTS HERE
    int execute_simulation_wrapped() { return execute_simulation_wrapped(std::make_index_sequence<sizeof...(Ts)>{}); }

private:
    template <std::size_t... Is>
    int execute_simulation_wrapped(std::index_sequence<Is...>) { return simulation(*std::get<Is>(stored_inputs)...); }
// MAGIC ENDS HERE

protected:
    virtual int simulation(const Ts&...){return 0;};
};

If you need an index_sequence , which is available in <utility> only since C++14, you can use the below implementation:如果你需要一个index_sequence ,它只在 C++14 之后的<utility>可用,你可以使用下面的实现:

template <std::size_t... Is>
struct index_sequence {};

template <std::size_t N, std::size_t... Is>
struct make_index_sequence_h : make_index_sequence_h<N - 1, N - 1, Is...> {};

template <std::size_t... Is>
struct make_index_sequence_h<0, Is...>
{
    using type = index_sequence<Is...>;
};

template <std::size_t N>
using make_index_sequence = typename make_index_sequence_h<N>::type;

DEMO演示

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

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