2016-10-12 1 views
3

以下は、私がしようとしているものの例です。私は問題を説明するためにそれを使用しています。このため基本パラメータテンプレートテンプレートパラメータとして使用するデュアルパラメータテンプレートから単一パラメータテンプレートを作成する方法

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

私はエラーを取得する:

/Work/Learn/04PartialTemplate/main.cpp:23:115: error: template argument 1 is invalid 
template <int STEP> class FixedDualStepper : public DualStepper<template <bool SIDE> using FT = Stepper<STEP, SIDE>> { 
                              ^
/Work/Learn/04PartialTemplate/main.cpp: In function ‘int main()’: 
/Work/Learn/04PartialTemplate/main.cpp:31:29: error: ‘class FixedDualStepper<5>’ has no member named ‘step’ 
    pair<int, int> x = stepper.step(10); 

は、私は望ましい効果を得るために

... : public DualStepper<???> 

に使用できる構文があります。私。 StepperSTEPに設定し、DualStepperのテンプレートテンプレートパラメータとして使用する単一のパラメータクラステンプレートを取得しますか?

答えて

1

これを行うには、構造体と宣言を使用できます。
それは最小限、実施例以下の:あなたのケースでは

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

を、それは次のようになります。あなたは歓迎されている@Jeevaka

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


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

。 – skypjack

関連する問題