When I try and "copy" a basic_ifstream<uint8_t> into a vector<uint8_t> I recieve a bad cast exception. If the uint8_t is replaced with "unsigned char" explicitly the problem persists, but if it is replaced with "char" the problem goes away. This has been confirmed not only on my system (GCC 4.1.1), but an Ubuntu system running GCC 4.0.3 and another running GCC 4.1.2. The code compiles and works fine under Microsoft Visual Studio .NET 2005. The g++ problem has not been tested on a Windows system. ------ #include <vector> #include <iterator> #include <fstream> #include <iostream> #include <sstream> #ifndef WIN32 #include "stdint.h" #else typedef unsigned char uint8_t; #endif using namespace std; int main(int argc, char **argv) { if (argc != 2) { cout << "Usage: " << argv[0] << " <log filename>" << endl; return 1; } // Open the input file basic_ifstream<uint8_t> inputFile(argv[1], ios::binary); if (inputFile.fail()) { cout << "Failed to open the input file" << endl; return 2; } // Read the file into a vector vector<uint8_t> inputData; istreambuf_iterator<uint8_t> inputDataStartIterator(inputFile), inputDataEndIterator; copy(inputDataStartIterator, inputDataEndIterator, back_inserter(inputData)); } ---- Command that triggers the bug: g++ test.cpp
Created attachment 13541 [details] Preprocessed C++ source code for my test application
This is known and we decided time ago not to do anything about it (you can find something either in Bugzilla or in the libstdc++ mailing list): a portable use of fstream with anything != char and wchar_t requires in general writing a custom codecvt.