考慮下面的程序:我應該在哪裏定義運算符>>用於我的std :: pair的專業化?
#include <iostream>
#include <iterator>
#include <vector>
#include <utility>
using namespace std; //just for convenience, illustration only
typedef pair<int, int> point; //this is my specialization of pair. I call it point
istream& operator >> (istream & in, point & p)
{
return in >> p.first >> p.second;
}
int main()
{
vector<point> v((istream_iterator<point>(cin)), istream_iterator<point>());
// ^^^ ^^^
//extra parentheses lest this should be mistaken for a function declaration
}
此故障後,會編譯,因爲作爲ADL認爲運營商>> std下它不考慮全球範圍內的任何更不論在性病中發現的經營者是否是可行的候選人與否。這很不方便。如果我將我的操作符>>的聲明放入命名空間std(這在技術上是非法的),代碼將按預期編譯。有沒有什麼辦法可以解決這個問題,而不是讓我自己的類變成point
,而不是將它定義爲std命名空間中模板的專門化?
預先感謝
你是不是專業'的std ::這裏pair'。我認爲這與模板化代碼解析的方式有關,而不是ADL本身。 –