यहां एक नमूना कोड है:क्या यह एक कंपाइलर बग है या यह मेरा कोड है?
#include <iostream>
#include <stdexcept>
#include <cstring>
#include <ctime>
#include <sstream>
using std::cout;
using std::endl;
std::size_t const BUF_SIZE(1000);
std::ostream& operator<<(std::ostream& os, std::tm const& rhs)
{
os << asctime(&rhs);
return os;
}
std::istream& operator>>(std::istream& is, std::tm& rhs)
{
while (is.peek() == ' ' || is.peek() == '\t')
{
is.get();
}
std::streampos curPos = is.tellg();
char buf[BUF_SIZE];
is.getline(buf, BUF_SIZE);
char* ptr = strptime(buf, "%D %T", &rhs);
if (ptr == 0)
{
throw std::runtime_error("strptime() failed!");
}
std::size_t processed = ptr - buf;
is.seekg(curPos + static_cast<std::streampos>(processed));
return is;
}
int main()
{
std::istringstream is("10101 07/09/12 07:30:00 123.24");
int uuid(0);
double price(0);
std::tm ptime; std::memset(&ptime, 0, sizeof(tm));
is >> uuid >> ptime >> price;
cout << "UUID: " << uuid << endl;
cout << "Time: " << ptime;
cout << "Price: " << price << endl;
}
जहाँ मैं struct टीएम के लिए < < और >> ऑपरेटरों को ओवरलोड करने की कोशिश कर रहा हूँ! अगर मैं जी के साथ ++ मेरी कोड संकलन और इसे चलाने, मैं मिलता है:
UUID: 10101
Time: Sun Jul 9 07:30:00 2012
Price: 123.24
बिल्कुल सही!
लेकिन, अगर मैं इसे बजना का उपयोग कर ++ संकलन, मैं मिलता है:
UUID: 10101
Time: Sun Jul 9 07:30:00 2012
Price: 0
ऊप्स!
क्या चल रहा है? यह क्लैंग के साथ एक समस्या है या जिस तरह से मैं आईट्रीम संसाधित कर रहा हूं?
ideone.com [gcc-4.3.4] (http://ideone.com/UDQEm) और [gcc-4.5.1] (http://ideone.com/NXZqK) पर उसी आउटपुट का उत्पादन करते हैं जो आप देखते हैं झुकाव के साथ। – Blastfurnace
क्या आपके लिए 'स्ट्रिपटाइम' काम आपके लिए है? यह एक गैर-मानक (POSIX) फ़ंक्शन है और मुझे यकीन नहीं है कि अगर क्लैंग का libC++ इसका समर्थन करता है। – dirkgently
'जीसीसी 4.6.3-1ubuntu5' _Perfect! _ आउटपुट देता है Reza मेरी x86-64 उबंटू सिस्टम पर रिपोर्ट करता है। – sarnold