テンプレートクラスの明示的なインスタンス化をテストするための短いプログラムを次のように作成しました。
#include <iostream>
template <class T>
struct less_than_comparable {
friend bool operator>=(T const& a, T const& b) {
return !(a < b);
}
};
class Point {
friend bool operator<(Point const& a, Point const& b) {
return a.x_ < b.x_;
}
public:
Point(int x) : x_(x) {}
private:
int x_;
};
template struct less_than_comparable<Point>;
//Here I explicitly instantiate a template class expecting that
//less_han_comparable<Point> would export opeartor>=() for class Point to the global
//namespace, and then the p1 >= p2 statement will work as expected.
int main() {
using namespace std;
Point p1(1), p2(2);
cout << (p1 < p2) << endl;
cout << (p1 >= p2) << endl; //But I have a compiler error here saying that
//no match for ‘operator>=’ in ‘p1 >= p2’
}
less_than_comparableからPointを継承する場合、コードはコンパイルに合格します。 しかし、私の質問は、明示的なインスタンス化を使用するとなぜ機能しないのかということです。 Ubuntu10.04で実行されているG++4.4.5を使用しています。コメントをいただければ幸いです。ありがとう。