可选的模板参数组合

Optional template parameter combinatorials

我有一组模板特化 (C++11) 使用下面的基本模板,其中 tPayLoadtReturnType存在:

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

第一个特化是当既没有 tPayLoadType 也没有 tReturnType:

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

第二个特化是当只有 tPayLoadType:

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

最后,我想要第 3 种情况(第 4 种组合),没有 tPayLoadType 但有 tReturnType,这就是问题所在弹出。我不能使用(我认为):

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

因为这与专业化 2 基本相同。任何人都知道如何做到这一点,如果可能的话???

/N

以下

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

给出输出

both void
back void
front void
base

正如人们所期望的那样。这是否回答了您的问题?