Home > OS >  Optional template parameter combinatorials
Optional template parameter combinatorials

Time:11-10

I have a set of template specializations (C 11) that uses the base template below, where both tPayLoad and tReturnType is present:

template <class tDerivedOperation, class tBridgeType, class tPayLoadType = void, class tReturnType = void> class OperationT ...

The 1st specialzation is when there is neither tPayLoadType nor tReturnType:

template <class tDerivedOperation, class tBridgeType> class OperationT<tDerivedOperation, tBridgeType, void, void> ...

The second specialization is when there is only tPayLoadType:

template <class tDerivedOperation, class tBridgeType, class tPayLoadType> class OperationT<tDerivedOperation, tBridgeType, tPayLoadType, void> ...

Finally, I want the 3rd case (4th combination), having no tPayLoadType but a tReturnType, and that is where the problem pops up. I can't use (I think):

template <class tDerivedOperation, class tBridgeType, class tReturnType> class OperationT<tDerivedOperation, tBridgeType, void, tReturnType> ...

since that is essentially the same as specialization 2. Anyone having an idea of how to do this, if it's even possible???

/N

CodePudding user response:

The following

#include <iostream>
template<class tDerivedOperation, class tBridgeType, class tPayLoadType = void, class tReturnType = void>
struct OperationT {
    static constexpr auto overload = "base";
};

template<class tDerivedOperation, class tBridgeType>
struct OperationT<tDerivedOperation, tBridgeType, void, void> {
    static constexpr auto overload = "both void";
};

template<class tDerivedOperation, class tBridgeType, class tPayLoadType>
struct OperationT<tDerivedOperation, tBridgeType, tPayLoadType, void> {
    static constexpr auto overload = "back void";
};

template<class tDerivedOperation, class tBridgeType, class tReturnType>
struct OperationT<tDerivedOperation, tBridgeType, void, tReturnType> {
    static constexpr auto overload = "front void";
};

int main() {
    std::cout << OperationT<int, int>::overload << '\n';
    std::cout << OperationT<int, int, int>::overload << '\n';
    std::cout << OperationT<int, int, void, int>::overload << '\n';
    std::cout << OperationT<int, int, int, int>::overload << '\n';
}

gives the output

both void
back void
front void
base

exactly as one would expect. Does this answer your question?

  • Related