Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/cplusplus/160.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/mercurial/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C++ c++;11将std::tuple解包到虚拟成员函数中_C++_C++11_Templates_Variadic Templates - Fatal编程技术网

C++ c++;11将std::tuple解包到虚拟成员函数中

C++ c++;11将std::tuple解包到虚拟成员函数中,c++,c++11,templates,variadic-templates,C++,C++11,Templates,Variadic Templates,完整故事: 我正在尝试构建一个看起来有点像这样的框架: #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:

完整故事:

我正在尝试构建一个看起来有点像这样的框架:

#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};
}
最后,我们要说的是:我们希望能够编写一个与家庭类型无关的函数,但仍然能够在模拟中调用
run

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

     some_household.run();  
}
总之:
run
调用虚拟方法的相关模板实例
execute\u simulation\u wrapped
,然后解压
存储的\u输入
,并将其提供给虚拟
模拟
函数,该函数为每个家庭定制了实现


我认为我应该问的问题:

所以,我想我已经把上面的大部分设置好了,但是我已经研究了很长时间了,我仍然无法理解
模拟包装器::execute\u simulation\u wrapped
函数如何调用
模拟
并将解包的元组
存储的输入作为参数包提供

我知道有很多问题和博客提供了关于如何使用解包元组调用常规函数的详细信息,但我还没有将其扩展到成员函数,特别是虚拟成员函数


TMP对我来说是新的,仍然是完全混乱的,所以非常感谢您给出明确的答案

这通常是在
索引顺序的帮助下完成的:

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

天哪,这真是又快又有用。让我看看!。。。是的,我想这就是我想要的。谢谢@dan man我不得不稍微修改代码的部分以使其可编译,但它本身并没有改变解决方案。请注意,
std::make_index_sequence
可能会被其别名
std::index_sequence_替换为
。为什么对
make_index_list
使用这种低效的线性递归方法?有具有对数实例化深度的二进制递归。
void play_simulation(simulation_base& some_household){
     // do some general stuff...

     some_household.run();  
}
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;};
};
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;