2013-01-21 7 views
8

मैं std::map में एक मूल्य के रूप में एक जंगम नहीं बल्कि copyable प्रकार रखना चाहते हैं। सिद्धांत का परीक्षण करने के लिए यहां कुछ सरल कोड दिया गया है।std :: map के लिए इस चलने योग्य प्रकार के साथ क्या गलत है?

#include <map> 

struct Foo 
{ 
    Foo(); 

    Foo (const Foo &) = delete; 
    Foo & operator = (const Foo &) = delete; 

    Foo (Foo &&) {} 
    Foo & operator = (Foo &&) {return *this;} 
}; 

int main() 
{ 
    std :: map <int, Foo> m; 

    m .insert (std :: make_pair (123, Foo())); 
} 

मैं g++ test.cpp --std=c++0x (जीसीसी Ubuntu 12.04 पर संस्करण 4.5.1) के साथ इस संकलन। जैसा कि नीचे दिखाया गया है वहां एक बड़ी बदसूरत त्रुटि है। समस्या क्या है?

In file included from /usr/local/lib/gcc/i686-pc-linux-gnu/4.5.1/../../../../include/c++/4.5.1/bits/stl_algobase.h:66:0, 
       from /usr/local/lib/gcc/i686-pc-linux-gnu/4.5.1/../../../../include/c++/4.5.1/bits/stl_tree.h:62, 
       from /usr/local/lib/gcc/i686-pc-linux-gnu/4.5.1/../../../../include/c++/4.5.1/map:60, 
       from test.cpp:1: test.cpp: In copy constructor ‘std::pair<const int, Foo>::pair(const std::pair<const int, Foo>&)’: /usr/local/lib/gcc/i686-pc-linux-gnu/4.5.1/../../../../include/c++/4.5.1/bits/stl_pair.h:72:5: instantiated from ‘std::_Rb_tree_node<_Val>::_Rb_tree_node(_Args&& ...) [with _Args = {const std::pair<const int, Foo>&}, _Val = std::pair<const int, Foo>]’ /usr/local/lib/gcc/i686-pc-linux-gnu/4.5.1/../../../../include/c++/4.5.1/ext/new_allocator.h:111:4: instantiated from ‘void __gnu_cxx::new_allocator<_Tp>::construct(_Tp*, 
_Args&& ...) [with _Args = {const std::pair<const int, Foo>&}, _Tp = std::_Rb_tree_node<std::pair<const int, Foo> >, _Tp* = std::_Rb_tree_node<std::pair<const int, Foo> >*]’ /usr/local/lib/gcc/i686-pc-linux-gnu/4.5.1/../../../../include/c++/4.5.1/bits/stl_tree.h:394:8: instantiated from ‘std::_Rb_tree_node<_Val>* std::_Rb_tree<_Key, _Val, 
_KeyOfValue, _Compare, _Alloc>::_M_create_node(_Args&& ...) [with _Args = {const std::pair<const int, Foo>&}, _Key = int, _Val = std::pair<const int, Foo>, _KeyOfValue = std::_Select1st<std::pair<const int, Foo> >, _Compare = std::less<int>, _Alloc = std::allocator<std::pair<const int, Foo> >, std::_Rb_tree_node<_Val>* = std::_Rb_tree_node<std::pair<const int, Foo> >*]’ /usr/local/lib/gcc/i686-pc-linux-gnu/4.5.1/../../../../include/c++/4.5.1/bits/stl_tree.h:899:42: instantiated from ‘std::_Rb_tree<_Key, _Val, _KeyOfValue, _Compare, 
_Alloc>::iterator std::_Rb_tree<_Key, _Val, _KeyOfValue, _Compare, _Alloc>::_M_insert_(const std::_Rb_tree_node_base*, const std::_Rb_tree_node_base*, const _Val&) [with _Key = int, _Val = std::pair<const int, Foo>, _KeyOfValue = std::_Select1st<std::pair<const int, Foo> >, _Compare = std::less<int>, _Alloc = std::allocator<std::pair<const int, Foo> >, std::_Rb_tree<_Key, _Val, _KeyOfValue, _Compare, _Alloc>::iterator = std::_Rb_tree_iterator<std::pair<const int, Foo> >, const std::_Rb_tree_node_base* = const std::_Rb_tree_node_base*]’ /usr/local/lib/gcc/i686-pc-linux-gnu/4.5.1/../../../../include/c++/4.5.1/bits/stl_tree.h:1191:65: instantiated from ‘std::pair<std::_Rb_tree_iterator<_Val>, bool> std::_Rb_tree<_Key, _Val, _KeyOfValue, _Compare, 
_Alloc>::_M_insert_unique(const _Val&) [with _Key = int, _Val = std::pair<const int, Foo>, _KeyOfValue = std::_Select1st<std::pair<const int, Foo> >, _Compare = std::less<int>, _Alloc = std::allocator<std::pair<const int, Foo> >]’ /usr/local/lib/gcc/i686-pc-linux-gnu/4.5.1/../../../../include/c++/4.5.1/bits/stl_map.h:501:41: instantiated from ‘std::pair<typename std::map<_Key, _Tp, _Compare, 
_Alloc>::_Rep_type::iterator, bool> std::map<_Key, _Tp, _Compare, _Alloc>::insert(const std::map<_Key, _Tp, _Compare, _Alloc>::value_type&) [with _Key = int, _Tp = Foo, _Compare = std::less<int>, _Alloc = std::allocator<std::pair<const int, Foo> >, typename std::map<_Key, _Tp, _Compare, _Alloc>::_Rep_type::iterator = std::_Rb_tree_iterator<std::pair<const int, Foo> >, std::map<_Key, 
_Tp, _Compare, _Alloc>::value_type = std::pair<const int, Foo>]’ test.cpp:20:43: instantiated from here test.cpp:7:2: error: deleted function ‘Foo::Foo(const Foo&)’ /usr/local/lib/gcc/i686-pc-linux-gnu/4.5.1/../../../../include/c++/4.5.1/bits/stl_pair.h:72:5: error: used here In file included from /usr/local/lib/gcc/i686-pc-linux-gnu/4.5.1/../../../../include/c++/4.5.1/map:60:0, 
       from test.cpp:1: /usr/local/lib/gcc/i686-pc-linux-gnu/4.5.1/../../../../include/c++/4.5.1/bits/stl_tree.h: In constructor ‘std::_Rb_tree_node<_Val>::_Rb_tree_node(_Args&& ...) [with _Args = {const std::pair<const int, Foo>&}, _Val = std::pair<const int, Foo>]’: /usr/local/lib/gcc/i686-pc-linux-gnu/4.5.1/../../../../include/c++/4.5.1/bits/stl_tree.h:136:49: note: synthesized method ‘std::pair<const int, Foo>::pair(const std::pair<const int, Foo>&)’ first required here 
+3

मैं एक "जवाब" के बाद से इस सवाल का बंद कर दिया है नहीं जोड़ सकते हैं, लेकिन यहाँ यह है: जीसीसी शिकायत कर रहा है कि यह 'std :: जोड़ी ' की प्रतिलिपि निर्माता का दृष्टांत नहीं कर सकते क्योंकि, 'फू' में कोई कॉपी कन्स्ट्रक्टर नहीं है। जीसीसी को 'std :: pair 'की प्रतिलिपि बनाने की आवश्यकता है क्योंकि ... लाइब्रेरी बग की वजह से: जीएनयू libstdC++ की' std :: pair' चाल-सक्षम नहीं है! आप libC++ या शायद एक अद्यतन libstdC++ का उपयोग करके इसके आसपास काम कर सकते हैं। आपका कोड लिखित रूप में सही है, और उदाहरण के लिए क्लैंग + libC++ के साथ संकलित होगा। – Quuxplusone

+0

यदि स्थानीय स्तर पर धक्का देने के लिए धक्का आता है तो उसे ठीक करने के लिए छोटा नहीं होना चाहिए? (LibstdC++ में ठीक है, मेरा मतलब है।) –

उत्तर

3

मैं भविष्य में संदर्भ के लिए कुछ विवरण जोड़ रहा: Quux के रूप में इस बग ने बताया std::pair के लिए a problem in the move constructor द्वारा जीसीसी 4.5.1 में कारण होता था।

आज के रूप में समस्या का समाधान हो गया है और आप या तो मानक पुस्तकालय का एक और संस्करण का उपयोग करके अपने संकलक संस्करण या वैकल्पिक हल नवीनीकृत करना चाहिए।

Your code works just fine

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