User account creation filtered due to spam.

Bug 31901 - bad_cast when using copy on istreambuf_iterator<unsigned char>
Summary: bad_cast when using copy on istreambuf_iterator<unsigned char>
Alias: None
Product: gcc
Classification: Unclassified
Component: libstdc++ (show other bugs)
Version: 4.1.1
: P3 normal
Target Milestone: ---
Assignee: Not yet assigned to anyone
Depends on:
Reported: 2007-05-11 19:03 UTC by batterseapower
Modified: 2007-05-11 20:22 UTC (History)
2 users (show)

See Also:
Host: i686-pc-linux-gnu
Target: i686-pc-linux-gnu
Build: /var/tmp/portage/sys-devel/gcc-4.1.1-r3/work/gcc-4.1.1/configure
Known to work:
Known to fail:
Last reconfirmed:

Preprocessed C++ source code for my test application (103.68 KB, text/plain)
2007-05-11 19:04 UTC, batterseapower

Note You need to log in before you can comment on or make changes to this bug.
Description batterseapower 2007-05-11 19:03:43 UTC
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"
typedef unsigned char       uint8_t;

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 (
        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
Comment 1 batterseapower 2007-05-11 19:04:49 UTC
Created attachment 13541 [details]
Preprocessed C++ source code for my test application
Comment 2 Paolo Carlini 2007-05-11 19:09:52 UTC
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.