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 行抱怨:
"没有名为 ' 的类型type
' in 'std::__1::enable_if<false, Vertex<Degree> &>
': 'enable_if
' 不能用于禁用此声明。"
如果我删除 Edge 的第二个构造函数,则不会出现错误。我想知道为什么,以及如何实现评论中描述的目的。
这是因为替换发生在外部(并且失败)直接上下文 https://stackoverflow.com/q/15260685/3953764。类型模板涉及的参数std::enable_if
应该来directly来自编译器在上下文需要存在函数/专业化时尝试实例化的模板,并且在此之前是未知的。否则,编译器可以随意拒绝您的代码。
一种可能的解决方法是将构造函数转换为模板,并将其参数默认为封闭类的模板参数的值:
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){}
DEMO http://coliru.stacked-crooked.com/a/8f1874604aab5811
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)