मैं सिर्फ g ++ 4.7 (बाद के स्नैपशॉट में से एक) के साथ -std = c ++ 11 सक्षम के साथ खेल रहा था। मैंने अपने कुछ मौजूदा कोड आधार और एक मामले को संकलित करने की कोशिश की जो कुछ हद तक मुझे भ्रमित करता है।
अगर कोई समझा सकता है कि क्या चल रहा है तो मैं सराहना करूंगा।
यहाँ कोड है:
#include <utility>
#include <iostream>
#include <vector>
#include <string>
int main ( )
{
std::string s = "abc";
// 1 ok
std::pair < std::string, int > a = std::make_pair ( s, 7 );
// 2 error on the next line
std::pair < std::string, int > b = std::make_pair < std::string, int > ( s, 7 );
// 3 ok
std::pair < std::string, int > d = std::pair < std::string, int > ( s, 7 );
return 0;
}
मैं समझता हूँ कि make_pair है मतलब (1) मामले (अगर मैं प्रकार निर्दिष्ट है, तो मैं भी उपयोग कर सकते हैं (3)) के रूप में प्रयोग की जाने वाली है, लेकिन मुझे समझ नहीं आता क्यों यह इस मामले में नाकाम रहने के है।
सटीक त्रुटि है:
test.cpp: In function ‘int main()’:
test.cpp:11:83: error: no matching function for call to ‘make_pair(std::string&, int)’
test.cpp:11:83: note: candidate is:
In file included from /gcc4.7/usr/local/lib/gcc/i686-pc-linux-gnu/4.7.0/../../../../include/c++/4.7.0/utility:72:0,
from test.cpp:1:
/gcc4.7/usr/local/lib/gcc/i686-pc-linux-gnu/4.7.0/../../../../include/c++/4.7.0/bits/stl_pair.h:274:5:
note: template<class _T1, class _T2> constexpr std::pair<typename std::__decay_and_strip<_T1>::__type, typename std::__decay_and_strip<_T2>::__type> std::make_pair(_T1&&, _T2&&)
/gcc4.7/usr/local/lib/gcc/i686-pc-linux-gnu/4.7.0/../../../../include/c++/4.7.0/bits/stl_pair.h:274:5:
note: template argument deduction/substitution failed:
test.cpp:11:83: note: cannot convert ‘s’ (type ‘std::string {aka std::basic_string<char>}’) to type ‘std::basic_string<char>&&’
फिर, यहाँ सवाल सिर्फ "क्या चल रहा है?" मुझे पता है कि मैं टेम्पलेट विनिर्देशन को हटाकर समस्या को ठीक कर सकता हूं, लेकिन मैं केवल यह जानना चाहता हूं कि कवर के तहत यहां क्या विफल हो रहा है।
- g ++ 4.4 इस कोड को बिना किसी समस्या के संकलित करता है।
- हटाना -dd = c ++ 11 भी बिना किसी समस्या के कोड के साथ संकलित करता है।
std::vector
समान है । कम से कम यह एक संकलक त्रुटि उत्पन्न करता है और शब्दार्थ में एक मौन परिवर्तन नहीं।