C++ 如何从双参数模板创建单参数模板以用作基类模板参数

C++ 如何从双参数模板创建单参数模板以用作基类模板参数,c++,templates,C++,Templates,下面是我试图做的一个例子。我只是用它来说明这个问题 #include <iostream> using namespace std; template <int STEP, bool SIDE> class Stepper { int step( int x ) { return SIDE ? x + STEP : x - STEP; } }; template <template <bool> typename ST

下面是我试图做的一个例子。我只是用它来说明这个问题

#include <iostream>
using namespace std;

template <int STEP, bool SIDE> class Stepper {
    int step( int x ) {
        return SIDE ? x + STEP : x - STEP;
    }
};

template <template <bool> typename STEPPER> class DualStepper {
    STEPPER<true> upStepper;
    STEPPER<false> downStepper;

    pair<int , int> step( int x ) {
        return pair<int , int>( upStepper.step( x ), downStepper.step( x ) );
    }
};


template <int STEP> class FixedDualStepper : public DualStepper<template <bool SIDE> using FT = Stepper<STEP, SIDE>> {

};


int main() {

    FixedDualStepper<5> stepper;
    pair<int, int> x = stepper.step( 10 );
    cout << x.first << '\t' << x.second << endl;

    return 0;
}
#包括
使用名称空间std;
模板类步进器{
整数步长(整数x){
返回侧?x+步进:x-步进;
}
};
模板类双步进器{
步进式步进器;
步进下降步进器;
成对步长(整数x){
返回对(上行步进(x)、下行步进(x));
}
};
模板类FixedDualStepper:公共双步进器{
};
int main(){
固定式步进器;
对x=步进电机。步骤(10);

cout您可以使用结构和使用声明来实现这一点。
下面是一个简单的工作示例:

template <int STEP, bool SIDE>
class Stepper {};

template<int S>
struct Type {
    template<bool b>
    using TStepper = Stepper<S, b>;
};

template<template<bool> class C>
void f() {}

int main() {
    f<Type<0>::TStepper>();
}
模板
类步进器{};
模板
结构类型{
模板
使用TStepper=步进器;
};
模板
void f(){}
int main(){
f();
}
在您的情况下,它将是:

template <template <bool> class STEPPER>
class DualStepper {
    // ....
};


template <int STEP>
class FixedDualStepper
    : public DualStepper<Type<STEP>::template TStepper> {
    // ...
};
模板
二级步进电机{
// ....
};
模板
类FixedDualStepper
:公共双步进电机{
// ...
};
template <int STEP, bool SIDE>
class Stepper {};

template<int S>
struct Type {
    template<bool b>
    using TStepper = Stepper<S, b>;
};

template<template<bool> class C>
void f() {}

int main() {
    f<Type<0>::TStepper>();
}
template <template <bool> class STEPPER>
class DualStepper {
    // ....
};


template <int STEP>
class FixedDualStepper
    : public DualStepper<Type<STEP>::template TStepper> {
    // ...
};