Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
588 views
in Technique[技术] by (71.8m points)

c++ - Is the move constructor of ifsteam implicitly deleted?

I have the following simple class:

class Source
{
public:
    Source() = default;
    Source(Source const&) = delete;
    Source(Source&&) = default;

    explicit Source(std::string const& fileName)
     : inputStream(fileName), path_(fileName)
    {}

    ~Source() = default;

    auto path() const -> std::string
    {
        return this->path_;
    }

    std::ifstream inputStream;
private:
    std::string path_;
};

auto main(int argc, char* argv[]) -> int
{
    Source source(Source("test.txt"));
    cout << source.path() << "
";

    return 0;
}

According to cppreference ifstream has a move constructor, but when I try to compile that with MinGW 4.7.2, I get the following error:

..srcmain.cpp:32:46: error: use of deleted function 'cy::Source::Source(cy::Source&&)' In file included from ..srcmain.cpp:10:0: source.hpp:28:5: note: 'cy::Source::Source(cy::Source&&)' is implicitly deleted because the default definition would be ill-formed: source.hpp:28:5: error: use of deleted function 'std::basic_ifstream::basic_ifstream(const std::basic_ifstream&)' c:mingwin../lib/gcc/mingw32/4.7.2/include/c++/fstream:420:11: note: 'std::basic_ifstream::basic_ifstream(const std::basic_ifstream&)' is implicitly deleted because the default definition would be ill-formed: c:mingwin../lib/gcc/mingw32/4.7.2/include/c++/fstream:420:11: error: use of deleted function 'std::basic_istream::basic_istream(const std::basic_istream&)'

Am I doing something wrong? Or the documentation of cppreference is inaccurate? Or GCC 4.7.2 has a bug?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

It turns out that the standard library implementation of GCC has not implemented yet the move and swap operation for the stream classes. See here for detail about the current status of C++11 features in the gcc standard library.

Thanks Jesse Good for giving the information and link.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...