我想专精getVector
成员函数,我正在尝试使用SFINAE。但它只有在Dim为3或更大时才有效。SFINAE模板成员超载
template <size_t Dim>
class Mat
{
...
template <size_t VDim, typename enable_if<(Dim > 1 && VDim == 0)>::type* = nullptr>
void getVectorBegin(const array<size_t, Dim - 1>& indexAfter) const;
template <size_t VDim, typename enable_if<(Dim > 2 && 0 < VDim && VDim < Dim-1)>::type* = nullptr>
void getVectorBegin(const array<size_t, VDim>& indexBefore, const array<size_t, Dim - VDim - 1>& indexAfter) const;
template <size_t VDim, typename enable_if<(Dim > 1 && VDim == Dim-1)>::type* = nullptr>
void getVectorBegin(const array<size_t, Dim - 1>& indexBefore) const;
};
Mat<3> m;
Mat<2> m; // error C2039: 'type': is not a member of 'std::enable_if<false,_Ty>'
您是否尝试将“Dim> x”检查作为最后的&&术语?我不确定编译器是否正确,或者它只是实现定义的,但它可能会缩短enable_if条件,并认为它的值不依赖于VDim,因此在成员声明实例化期间发生编译器错误(即,在Mat <2>瞬间点) –
有些东西你没有向我们展示。如果不使用模板类的成员函数,则不会实例化。你能告诉我们呼叫网站吗? – papagaga
@papagaga这不完全正确,成员函数声明(但不是它们的定义)在类模板时被实例化。因此,如果声明在类模板参数中包含一个仅依赖于值的表达式,并且表达式结果不正确,那么即使该函数未被“使用”也会产生错误... –