2013-05-14 12 views
6

जबकि उत्तर दिया this SO question (बेहतर this "duplicate" पढ़ें), मैं एक ऑपरेटर की निर्भर नाम संकल्प के लिए निम्न समाधान के साथ आया था:आश्रित नाम संकल्प और नाम स्थान एसटीडी/मानक पुस्तकालय

[temp.dep.res]/1:

निर्भर नामों को हल करने के लिए, निम्न स्रोतों से नाम माना जाता है:

  • घोषणा कि टेम्पलेट की परिभाषा के बिंदु पर दिखाई दे रहे हैं।
  • तत्काल संदर्भ (14.6.4.1) और परिभाषा संदर्भ से दोनों फ़ंक्शन तर्कों के प्रकार से जुड़े नामस्थानों से घोषणाएं।
#include <iostream> 
#include <utility> 

// this operator should be called from inside `istream_iterator` 
std::istream& operator>>(std::istream& s, std::pair<int,int>& p) 
{ 
    s >> p.first >> p.second; 
    return s; 
} 

// include definition of `istream_iterator` only after declaring the operator 
// -> temp.dep.res/1 bullet 1 applies?? 
#include <iterator> 

#include <map> 
#include <fstream> 

int main() 
{ 
    std::ifstream in("file.in"); 

    std::map<int, int> pp; 
    pp.insert(std::istream_iterator<std::pair<int, int>>{in}, 
       std::istream_iterator<std::pair<int, int>>{}); 
} 

लेकिन बजना ++ 3.2 और जी ++ 4.8 इस ऑपरेटर (नाम संकल्प) भी नहीं मिलता है।

<iterator> शामिल नहीं है "टेम्पलेट की परिभाषा बिंदु" istream_iterator को परिभाषित नहीं करता है?

संपादित करें: Andy Prowl बताते हैं, यह मानक लाइब्रेरी के साथ कोई संबंध नहीं है, बल्कि नाम देखने के साथ (कई operator>> साथ स्टैंडर्ड लाइब्रेरी नकल उतार, कम से कम नकली istream का नाम स्थान में एक के बाद साबित हो सकता है)।


EDIT2: का संभावित हल का उपयोग करते हुए [basic.lookup.argdep]/2 गोली 2

#include <iostream> 
#include <utility> 

// can include <iterator> already here, 
// as the definition of a class template member function 
// is only instantiated when the function is called (or explicit instantiation) 
// (make sure there are no relevant instantiations before the definition 
// of the operator>> below) 
#include <iterator> 

struct my_int 
{ 
    int m; 
    my_int() : m() {} 
    my_int(int p) : m(p) {} 
    operator int() const { return m; } 
}; 

// this operator should be called from inside `istream_iterator` 
std::istream& operator>>(std::istream& s, std::pair<my_int,my_int>& p) 
{ 
    s >> p.first.m >> p.second.m; 
    return s; 
} 

#include <map> 
#include <fstream> 

int main() 
{ 
    std::ifstream in("file.in"); 

    std::map<int, int> pp; 
    pp.insert(std::istream_iterator<std::pair<my_int, my_int>>{in}, 
       std::istream_iterator<std::pair<my_int, my_int>>{}); 
} 

बेशक, आप भी अपनी खुद की pair प्रकार, उपयोग कर सकते हैं जब तक वैकल्पिक हल एक परिचय के रूप में कस्टम operator>> के नामस्थान में संबद्ध वर्ग।

उत्तर

3

समस्या यहाँ उस बिंदु जहां operator >> करने के लिए अपने कॉल की जा रही है कहीं न कहीं std नाम स्थान के अंदर है, और नाम स्थान जहां तर्क के प्रकार रहते std है।

परंतु जहां कॉल होती है या नाम स्थान जहां तर्क के प्रकार रहते हैं (दोनों इस मामले में std नाम स्थान हैं), कोई फर्क नहीं पड़ता कि क्या यह व्यावहारिक है या अधिभार के लिए नहीं संकलक या तो नाम स्थान में एक operator >> पा सकते हैं संकल्प ( नाम लुकअप के बाद किया जाता है), यह पैरेंट नेमस्पेस में operator >> के अधिक अधिभार की तलाश में परेशान नहीं होगा।

दुर्भाग्य से, आपके operator >> वैश्विक नामस्थान में रहते हैं और इसलिए, नहीं मिला है।

+0

क्या आप एक संदर्भ प्रदान कर सकते हैं? :) मैं इसे मानक – dyp

+0

@DyP में देखने के लिए उत्सुक हूं: मेरे उत्तर को संपादन की आवश्यकता है;) मैं इस पर काम करूंगा –

+0

ठीक है मुझे यह मिला :) [basic.lookup.unqual]/1; संबंधित नेमस्पेस/तर्क-निर्भर लुकअप में लुकअप यहां काम नहीं करता है क्योंकि दोनों प्रकार 'नेमस्पेस std' से हैं। – dyp

संबंधित मुद्दे