5
नीचे दिए गए कोड पर विचार करें। मैं) समझ में नहीं क्यों मेरे जीसीसी संकलक परोक्ष MyClass :: ऑपरेटर स्ट्रिंग (इस्तेमाल करने की कोशिश नहीं कर रहा है, हालांकि MyClass :: ऑपरेटर स्ट्रिंग() परिभाषित किया गया है:std :: string :: ऑपरेटर +() के साथ स्पष्ट रूप से Myclass :: ऑपरेटर स्ट्रिंग() को कॉल करना क्यों आवश्यक है?
#include <string>
using namespace std;
struct T {
};
T operator+(const T& a, const T&b) { }
struct Myclass {
operator string() const { }
operator T() const { }
};
int main() {
T a;
string b;
Myclass c;
c + a; // OK
c.operator string() + b; // OK
c + b; // Not OK
/* The above line does not compile, although in <string> I see:
basic_string<_CharT, _Traits, _Alloc>
operator+(const basic_string<_CharT, _Traits, _Alloc>& __lhs,
const basic_string<_CharT, _Traits, _Alloc>& __rhs)
*/
}
मुझे विश्वास है कि कारण यह है कि 'std :: ऑपरेटर +' एक फ़ंक्शन टेम्पलेट है, न कि कोई फ़ंक्शन। –