Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to create a uni-parameter template from a dual-parameter template to use as a base class template template parameter

Tags:

c++

templates

Below is an example of what I am trying to do. I am just using it to illustrate the problem.

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

For this I get the error:

/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 );

Is there a syntax I could use in

... : public DualStepper< ??? >

to get the desired effect. I.e. set the first parameter of Stepper to STEP and get a single parameter class template to use as a template template parameter for DualStepper?

like image 809
Aelian Avatar asked Jan 21 '26 17:01

Aelian


1 Answers

You can use a struct and an using declaration to do that.
It follows a minimal, working example:

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

In your case, it would be:

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


template <int STEP>
class FixedDualStepper
    : public DualStepper<Type<STEP>::template TStepper> {
    // ...
};
like image 131
skypjack Avatar answered Jan 24 '26 09:01

skypjack



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!