4

我在尝试 Visual Studio 10(Beta 2)时正在编写一些简单的点代码,并且我已经在我希望 SFINAE 启动的地方找到了这个代码,但它似乎没有:

template<typename T>
struct point {
    T x, y;
    point(T x, T y) : x(x), y(y) {}
};

template<typename T, typename U>
struct op_div {
    typedef decltype(T() / U()) type;
};

template<typename T, typename U>
point<typename op_div<T, U>::type>
operator/(point<T> const& l, point<U> const& r) {
    return point<typename op_div<T, U>::type>(l.x / r.x, l.y / r.y);
}

template<typename T, typename U>
point<typename op_div<T, U>::type>
operator/(point<T> const& l, U const& r) {
    return point<typename op_div<T, U>::type>(l.x / r, l.y / r);
}

int main() {
    point<int>(0, 1) / point<float>(2, 3);
}

这给error C2512: 'point<T>::point' : no appropriate default constructor available

鉴于它是一个测试版,我用在线 Comeau 编译器做了一个快速的健全性检查,它同意一个相同的错误,所以看起来这种行为是正确的,但我不明白为什么。

在这种情况下,一些解决方法是简单地内联decltype(T() / U()), 给点类一个默认构造函数,或者在完整的结果表达式上使用 decltype ,但是我在尝试简化使用 op_div 版本时遇到的错误时遇到了这个错误不需要默认构造函数*,所以我宁愿修正我对 C++ 的理解,而不是只做有效的事情。

谢谢!


*: 原本的:

template<typename T, typename U>
struct op_div {
    static T t(); static U u();
    typedef decltype(t() / u()) type;
};

这给出了error C2784: 'point<op_div<T,U>::type> operator /(const point<T> &,const U &)' : could not deduce template argument for 'const point<T> &' from 'int',也给出了point<T> / point<U>过载。

4

2 回答 2

2

不是100%确定。看来编译器需要实例化两个重载以确定哪个更好,但是在尝试使用 and 实例化另一个 op_div 时T = intU = point<float>这会导致 SFINAE 未涵盖的错误(错误不是 op_div 没有类型在这种情况下,但无法确定该类型)。

boost::disable_if如果第二种类型是点 ( ) ,您可以尝试禁用第二种重载。

此外,似乎可行的是推迟返回类型声明(取消 op_div 结构,但取决于编译器支持哪些 C++0x 功能):

template<typename T, typename U>
auto
operator/(point<T> const& l, point<U> const& r) -> point<decltype(l.x / r.x)> {
    return {l.x / r.x, l.y / r.y};
}

template<typename T, typename U>
auto
operator/(point<T> const& l, U const& r) -> point<decltype(l.x / r)> {
    return {l.x / r, l.y / r};
}
于 2009-10-22T12:49:16.197 回答
-1

我会说你的错误在这里:

template<typename T>
struct point {
    T x, y;
    point(T x, T y) : x(x), y(y) {}
};

将您的结构定义更改为:

template<typename T>
struct point<T> {
    T x, y;
    point(T x, T y) : x(x), y(y) {}
};

如果要使用泛型类型 T,则需要在定义中指定它。

于 2009-10-22T12:08:05.410 回答