2012-06-12 15 views
7

は、ここでのサンプルコードです:私は、構造体tmのため< <と>>演算子をオーバーロードしようとしているこれはコンパイラのバグですか、それとも自分のコードですか?

#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; 
} 

! 私はグラムで私のコードをコンパイル++、それを実行すると、私が手:

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 

OOPSを!

何が起こっていますか? clangの問題ですか、それともistreamを処理しているのですか?

+0

ideone.com [gcc-4.3.4](http://ideone.com/UDQEm)と[gcc-4.5.1](http://ideone.com/NXZqK)では、同じ出力が表示されますclangと。 – Blastfurnace

+0

'strptime'はあなたのために動いていますか?これは非標準(POSIX)関数であり、clangのlibC++でサポートされているかどうかはわかりません。 – dirkgently

+0

'gcc 4.6.3-1ubuntu5'は私のx86-64 Ubuntuシステムで_Perfect!_出力Rezaレポートを出力します。 – sarnold

答えて

9

私はこの再現(G ++ 4.7.0および打ち鳴らす++ 3.1のlibC++と - SVN)することができたと簡単なデバッグ・セッションは、その後何とかfailbitを設定するseekgを引き起こすgetlineeofbitを設定++その打ち鳴らす(正常である)、を示しました。これはseekg first clears eofbit(§27.7.2.3/ 41)

は、周りの仕事のどこgetlineseekgis.clear()を挿入することを考えると、バグのように聞こえます。

+2

PS:この言葉はC++ 11で新しく書かれたもので、C++ 03はそのことを言っておらず、実際にはeofから離れていくことを疑問に思っていました:[LWG issue 342](http://cplusplus.github。 com/LWG/lwg-closed.html#342) – Cubbi

+3

PS/2:提出済み:http://llvm.org/bugs/show_bug.cgi?id=13089 – Cubbi

関連する問題