枚举模板函数通常称为

时间:2018-06-15 13:31:32

标签: c++ templates

我有这样的工作流步骤函数,它们由枚举模板化:

template< eSimulationMethod SIM > void DoStep1( const ParamHolder& in, OutputHolder& out );
template< eSimulationMethod SIM > void DoStep2( const ParamHolder& in, OutputHolder& out );

我希望能够以类似工作流的方式调用这些(这包括将运行时枚举转换为交换机中的编译时参数)。我试过这个:

template< template<eSimulationMethod> class WSTEP >
void DoApply( WSTEP step, const ParamHolder& in, OutputHolder& out )
{
    switch(   in.sim_ )
    {
    case ehs:   step< ehs            >( in, out ); break;
    default:    step< enohsnoreorder >( in, out ); break;
    }
}

通话地点:

DoApply( DoStep1, in, out );

这不编译。如何在这种情况下使用模板模板?有没有更好的方法来提出运行时到编译时转换和通用性的组合?

1 个答案:

答案 0 :(得分:0)

您无法传递模板化的功能模板。 你可以在一个类中包装:

template< eSimulationMethod SIM > void DoStep1( const ParamHolder& in, OutputHolder& out );
template< eSimulationMethod SIM > void DoStep2( const ParamHolder& in, OutputHolder& out );

struct DoStep1Runner
{
    template< eSimulationMethod SIM >
    void run(const ParamHolder& in, OutputHolder& out) const
    {
        DoStep1<SIM>(in, out);
    }
};

struct DoStep2Runner
{
    template< eSimulationMethod SIM >
    void run(const ParamHolder& in, OutputHolder& out) const
    {
        DoStep2<SIM>(in, out);
    }
};

然后将该对象传递给您的方法:

template <typename T>
void DoApply(const T& runner, const ParamHolder& in, OutputHolder& out )
{
    switch(   in.sim_ )
    {
        case ehs:   runner.template run<           ehs>(in, out); break;
        default:    runner.template run<enohsnoreorder>(in, out); break;
    }
}

通话地点:

DoApply(DoStep1Runner{}, in, out);