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>
過負荷に対しても。