模板特化需要重复参数

时间:2016-12-19 12:17:18

标签: c++ c++11 templates

我想基于参数which专门化模板,但如果我删除template < bool which2 = which >编译失败。为什么? 如何避免重复?

#include <iostream>

template < typename T, bool which >
class Node {
    T key;
public:
    template < bool which2 = which >
    typename std::enable_if < which2 == false, bool >::type operator < ( const Node & second );
    template < bool which2 = which >
    typename std::enable_if < which2 == true, bool >::type operator < ( const Node & second );

    Node ( ) {
        (*this) < (*this);
    }
};

template < class T, bool which >
template < bool which2 >
typename std::enable_if < which2 == false, bool >::type Node < T, which >::operator < ( const Node & second ) {
    std::cout << "false" << std::endl;
    return false;
}

template < class T, bool which >
template < bool which2 >
typename std::enable_if < which2 == true, bool >::type Node < T, which >::operator < ( const Node & second ) {
    std::cout << "true" << std::endl;
    return false;
}

template < typename T, bool which >
class BH {
    Node < T, which > node;
};

int main ( ) {
    BH < int, true > aa;
    return 0;
}

0 个答案:

没有答案