Bug 53626 - [C++11] move assignment for ifstream
Summary: [C++11] move assignment for ifstream
Status: RESOLVED FIXED
Alias: None
Product: gcc
Classification: Unclassified
Component: libstdc++ (show other bugs)
Version: 4.7.0
: P3 normal
Target Milestone: 5.0
Assignee: Jonathan Wakely
URL:
Keywords:
: 57677 (view as bug list)
Depends on:
Blocks: 54316
  Show dependency treegraph
 
Reported: 2012-06-10 19:45 UTC by Mikhail Semenov
Modified: 2014-09-22 13:45 UTC (History)
3 users (show)

See Also:
Host:
Target:
Build:
Known to work:
Known to fail:
Last reconfirmed: 2014-09-19 00:00:00


Attachments
The program (372 bytes, text/plain)
2012-06-10 19:45 UTC, Mikhail Semenov
Details

Note You need to log in before you can comment on or make changes to this bug.
Description Mikhail Semenov 2012-06-10 19:45:56 UTC
Created attachment 27599 [details]
The program

The move assignment operator should be provided for ifstream.
The following program fails to compile. It should print out the contents of a text file, whose first line starts with "My File".

#include <iostream>
#include <string>
#include <fstream>

std::ifstream OpenMyFile(const std::string& filename)
{
    std::ifstream file(filename);
    if (!file.good())
    {
        file.close();
        return file;
    }
    std::string s;
    std::getline(file, s);
    if (s.substr(0,7) != "My File")
    {
        file.close();
        file.setstate(std::ios_base::failbit);
        return file;
    }
    return file;
}

int main(int count, char *args[])
{
    if (count < 2)
    {
        std::cout << "Incorrect number of parameters" << std::endl;
        return -1;
    }
    std::ifstream myFile = OpenMyFile(args[1]);
    if (myFile.good())
    {
        while (!myFile.eof())
        {
            std::string s;
            std::getline(myFile,s);
            std::cout << s << std::endl;
        }
    }
    else
    {
        std::cout << "*** FILE ERROR ***" << std::endl;
    }
    return 0;
}
Comment 1 Jonathan Wakely 2012-06-10 21:20:27 UTC
Yep, this is documented as missing: http://gcc.gnu.org/onlinedocs/libstdc++/manual/status.html#status.iso.2011
Comment 2 Paolo Carlini 2013-06-22 19:10:57 UTC
*** Bug 57677 has been marked as a duplicate of this bug. ***
Comment 3 Jonathan Wakely 2014-09-22 13:34:41 UTC
Author: redi
Date: Mon Sep 22 13:34:09 2014
New Revision: 215463

URL: https://gcc.gnu.org/viewcvs?rev=215463&root=gcc&view=rev
Log:
Make streams movable and swappable.

	PR libstdc++/54316
	PR libstdc++/53626
	* config/abi/pre/gnu.ver: Add new exports.
	* config/io/basic_file_stdio.h (__basic_file): Support moving and
	swapping.
	* include/bits/basic_ios.h (basic_ios::move, basic_ios::swap):
	Likewise.
	* include/bits/ios_base.h (ios_base::_M_move, ios_base::_M_swap):
	Likewise.
	* include/bits/fstream.tcc (basic_filebuf): Likewise.
	* include/bits/move.h (__exchange): Define for C++11 mode.
	* include/ext/stdio_filebuf.h (stdio_filebuf): Support moving and
	swapping.
	* include/ext/stdio_sync_filebuf.h (stdio_sync_filebuf): Likewise.
	* include/std/fstream (basic_filebuf, basic_ifstream, basic_ofstream,
	basic_fstream): Likewise.
	* include/std/ios: Remove whitespace.
	* include/std/istream (basic_istream, basic_iostream): Support moving
	and swapping.
	* include/std/ostream (basic_ostream): Likewise.
	* include/std/sstream (basic_stringbuf, basic_istringstream,
	basic_ostringstream, basic_stringstream): Likewise.
	* include/std/streambuf (basic_streambuf): Do not default copy
	constructor and assignment on first declaration.
	* include/std/utility (exchange): Forward to __exchange.
	* testsuite/27_io/basic_filebuf/cons/char/copy_neg.cc: New.
	* src/c++11/Makefile.am: Add stream-related files.
	* src/c++11/Makefile.in: Regenerate.
	* src/c++11/ext11-inst.cc (stdio_filebuf, stdio_sync_filebuf):
	New file for explicit instantiation definitions.
	* src/c++11/ios.cc: Move from src/c++98 to here.
	(ios_base::_M_move, ios_base::_M_swap): Define.
	* src/c++11/ios-inst.cc: Move from src/c++98 to here.
	* src/c++11/iostream-inst.cc: Likewise.
	* src/c++11/istream-inst.cc: Likewise.
	* src/c++11/ostream-inst.cc: Likewise.
	* src/c++11/sstream-inst.cc: Likewise.
	* src/c++11/streambuf-inst.cc: Likewise.
	* src/c++98/Makefile.am: Remove stream-related files.
	* src/c++98/Makefile.in: Regenerate.
	* src/c++98/ext-inst.cc (stdio_filebuf): Remove explicit
	instantiations.
	* src/c++98/misc-inst.cc (stdio_sync_filebuf): Likewise.
	* src/c++98/ios-inst.cc: Move to src/c++11/.
	* src/c++98/ios.cc: Move to src/c++11/.
	* src/c++98/iostream-inst.cc: Likewise.
	* src/c++98/istream-inst.cc: Likewise.
	* src/c++98/ostream-inst.cc: Likewise.
	* src/c++98/sstream-inst.cc: Likewise.
	* src/c++98/streambuf-inst.cc: Likewise.
	* testsuite/27_io/basic_filebuf/cons/char/copy_neg.cc: New.
	* testsuite/27_io/basic_fstream/cons/move.cc: New.
	* testsuite/27_io/basic_fstream/assign/1.cc: New.
	* testsuite/27_io/basic_ifstream/cons/move.cc: New.
	* testsuite/27_io/basic_ifstream/assign/1.cc: New.
	* testsuite/27_io/basic_istringstream/assign/1.cc: New.
	* testsuite/27_io/basic_istringstream/cons/move.cc: New.
	* testsuite/27_io/basic_ofstream/cons/move.cc: New.
	* testsuite/27_io/basic_ofstream/assign/1.cc: New.
	* testsuite/27_io/basic_ostringstream/assign/1.cc: New.
	* testsuite/27_io/basic_ostringstream/cons/move.cc: New.
	* testsuite/27_io/basic_stringstream/assign/1.cc: New.
	* testsuite/27_io/basic_stringstream/cons/move.cc: New.

Added:
    trunk/libstdc++-v3/src/c++11/ext11-inst.cc
      - copied, changed from r215458, trunk/libstdc++-v3/src/c++98/ios-inst.cc
    trunk/libstdc++-v3/src/c++11/ios-inst.cc
      - copied, changed from r215458, trunk/libstdc++-v3/src/c++98/ios-inst.cc
    trunk/libstdc++-v3/src/c++11/ios.cc
      - copied, changed from r215458, trunk/libstdc++-v3/src/c++98/ios.cc
    trunk/libstdc++-v3/src/c++11/iostream-inst.cc
      - copied, changed from r215458, trunk/libstdc++-v3/src/c++98/iostream-inst.cc
    trunk/libstdc++-v3/src/c++11/istream-inst.cc
      - copied, changed from r215458, trunk/libstdc++-v3/src/c++98/istream-inst.cc
    trunk/libstdc++-v3/src/c++11/ostream-inst.cc
      - copied, changed from r215458, trunk/libstdc++-v3/src/c++98/ostream-inst.cc
    trunk/libstdc++-v3/src/c++11/sstream-inst.cc
      - copied, changed from r215458, trunk/libstdc++-v3/src/c++98/sstream-inst.cc
    trunk/libstdc++-v3/src/c++11/streambuf-inst.cc
      - copied, changed from r215458, trunk/libstdc++-v3/src/c++98/streambuf-inst.cc
    trunk/libstdc++-v3/testsuite/27_io/basic_filebuf/cons/char/copy_neg.cc
    trunk/libstdc++-v3/testsuite/27_io/basic_fstream/assign/
    trunk/libstdc++-v3/testsuite/27_io/basic_fstream/assign/1.cc
    trunk/libstdc++-v3/testsuite/27_io/basic_fstream/cons/move.cc
    trunk/libstdc++-v3/testsuite/27_io/basic_ifstream/assign/
    trunk/libstdc++-v3/testsuite/27_io/basic_ifstream/assign/1.cc
    trunk/libstdc++-v3/testsuite/27_io/basic_ifstream/cons/move.cc
    trunk/libstdc++-v3/testsuite/27_io/basic_istringstream/assign/
    trunk/libstdc++-v3/testsuite/27_io/basic_istringstream/assign/1.cc
    trunk/libstdc++-v3/testsuite/27_io/basic_istringstream/cons/move.cc
    trunk/libstdc++-v3/testsuite/27_io/basic_ofstream/assign/
    trunk/libstdc++-v3/testsuite/27_io/basic_ofstream/assign/1.cc
    trunk/libstdc++-v3/testsuite/27_io/basic_ofstream/cons/move.cc
    trunk/libstdc++-v3/testsuite/27_io/basic_ostringstream/assign/
    trunk/libstdc++-v3/testsuite/27_io/basic_ostringstream/assign/1.cc
    trunk/libstdc++-v3/testsuite/27_io/basic_ostringstream/cons/move.cc
    trunk/libstdc++-v3/testsuite/27_io/basic_stringstream/assign/
    trunk/libstdc++-v3/testsuite/27_io/basic_stringstream/assign/1.cc
    trunk/libstdc++-v3/testsuite/27_io/basic_stringstream/cons/move.cc
Removed:
    trunk/libstdc++-v3/src/c++98/ios-inst.cc
    trunk/libstdc++-v3/src/c++98/ios.cc
    trunk/libstdc++-v3/src/c++98/iostream-inst.cc
    trunk/libstdc++-v3/src/c++98/istream-inst.cc
    trunk/libstdc++-v3/src/c++98/ostream-inst.cc
    trunk/libstdc++-v3/src/c++98/sstream-inst.cc
    trunk/libstdc++-v3/src/c++98/streambuf-inst.cc
Modified:
    trunk/libstdc++-v3/ChangeLog
    trunk/libstdc++-v3/config/abi/pre/gnu.ver
    trunk/libstdc++-v3/config/io/basic_file_stdio.h
    trunk/libstdc++-v3/include/bits/basic_ios.h
    trunk/libstdc++-v3/include/bits/fstream.tcc
    trunk/libstdc++-v3/include/bits/ios_base.h
    trunk/libstdc++-v3/include/bits/move.h
    trunk/libstdc++-v3/include/ext/stdio_filebuf.h
    trunk/libstdc++-v3/include/ext/stdio_sync_filebuf.h
    trunk/libstdc++-v3/include/std/fstream
    trunk/libstdc++-v3/include/std/ios
    trunk/libstdc++-v3/include/std/istream
    trunk/libstdc++-v3/include/std/ostream
    trunk/libstdc++-v3/include/std/sstream
    trunk/libstdc++-v3/include/std/streambuf
    trunk/libstdc++-v3/include/std/utility
    trunk/libstdc++-v3/src/c++11/Makefile.am
    trunk/libstdc++-v3/src/c++11/Makefile.in
    trunk/libstdc++-v3/src/c++98/Makefile.am
    trunk/libstdc++-v3/src/c++98/Makefile.in
    trunk/libstdc++-v3/src/c++98/ext-inst.cc
    trunk/libstdc++-v3/src/c++98/misc-inst.cc
Comment 4 Jonathan Wakely 2014-09-22 13:45:43 UTC
Fixed for GCC 5