为什么编译器说:'enable_if'不能用于禁用此声明

why compiler said: 'enable_if' cannot be used to disable this declaration

本文关键字:用于 声明 不能 enable 编译器 为什么 if      更新时间:2023-10-16
template <bool Cond, typename Type = void>
using Enable_if = typename std::enable_if<Cond, Type>::type;
class Degree;
template <typename T>
constexpr inline bool Is_Degree() {
    return std::is_base_of<Degree, T>::value;
}
class Degree {
public:
    std::size_t inDeg = 0;
};
template <typename Satellite = Degree>
class Vertex: public Satellite {
public:
    explicit Vertex(int num): n(num) {}
private:
    std::size_t n;
};
template <typename Satellite = Degree>
class Edge {
public:
    // i want have different constructor depending on 
    // whether Vertex is (directly or indirectly) derived from Degree
    Edge(Enable_if<Is_Degree<Satellite>(), Vertex<Satellite> &>fromVertex,
        Vertex<Satellite> &toVertex)
        : from(fromVertex), to(toVertex){ ++to.inDeg; }
    Edge(Enable_if<!Is_Degree<Satellite>(), Vertex<Satellite> &>fromVertex, 
        Vertex<Satellite> &toVertex)
        : from(fromVertex), to(toVertex){}
private:
    Vertex<Satellite> &from;
    Vertex<Satellite> &to;
};

编译器在第2行报错:

"在' std::__1::enable_if<false, Vertex<Degree> &> ': ' enable_if '中没有名为' type '的类型不能用于禁用此声明。"

如果我删除Edge的第二个构造函数没有错误。我想知道为什么,以及如何达到我在评论中描述的目的。

这是因为替换发生在直接上下文之外(并且失败)。std::enable_if中涉及的类型模板参数应该直接来自一个模板,当一个函数/专门化被上下文要求存在时,编译器试图实例化这个模板,并且在此之前是未知的。否则,编译器可以自由地拒绝你的代码。

一种可能的解决方法是将构造函数转换为模板,并将其形参默认为封闭类的模板形参的值:
template <typename S = Satellite>
//                 ^-----v
Edge(Enable_if<Is_Degree<S>(), Vertex<Satellite> &>fromVertex,
    Vertex<Satellite> &toVertex)
    : from(fromVertex), to(toVertex){ ++to.inDeg; }
template <typename S = Satellite>
//                 ^------v
Edge(Enable_if<!Is_Degree<S>(), Vertex<Satellite> &>fromVertex, 
    Vertex<Satellite> &toVertex)
    : from(fromVertex), to(toVertex){}