c++templatespolicy-based-design

multiple use of policies in policy-based design


i have a class template roundtrip which takes two policies. as long as they are different, everything is fine, but using one policy twice leads to compilation errors.

Example:

#include <iostream>

class walk {
protected:
    void move() {
        std::cout<<"i'm walking."<<std::endl;
    }
};

class car {
protected:
    void move() {
        std::cout<<"i'm driving in a car."<<std::endl;
    }
};

template<typename S, typename T>
class roundtrip : private S, private T {
public:
    void printSchedule(void) {
        std::cout<<"away: ";
        S::move();

        std::cout<<"return: ";
        T::move();
    }
};

int main(void){
    roundtrip<walk,car> LazyTrip;
    LazyTrip.printSchedule();

    roundtrip<car,car> VeryLazyTrip; // ERROR: error: duplicate base type ‘walk’ invalid
    VeryLazyTrip.printSchedule();

    return 0;
}

how can that be resolved? or is there a better design to achieve the same behaviour?

EDIT: i added wrappers to the policies, the user interface does not change. what do you think about this solution, is it a clean design?

template<typename T>
class outbound : private T {
protected:
    void moveOutbound(void) {
        T::move();
    }
};

template<typename T>
class inbound : private T {
protected:
    void moveInbound(void) {
        T::move();
    }
};

template<typename S, typename T>
class roundtrip : private outbound<S>, private inbound<T> {
public:
    void printSchedule(void) {
        std::cout<<"away: ";
        this->moveOutbound();

        std::cout<<"return: ";
        this->moveInbound();
    }
};

Solution

  • Instead of inheriting, you could add data members to roundtrip, but the functions in walk and car are currently protected. If you cannot make the functions in walk and car public, you could: