gpt4 book ai didi

c++ - ifstream的move构造函数是否被隐式删除?

转载 作者:IT老高 更新时间:2023-10-28 23:18:03 31 4
gpt4 key购买 nike

我有以下简单的类:

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() << "\n";

return 0;
}

根据cppreference ifstream 有一个 move 构造函数,但是当我尝试使用 MinGW 4.7.2 编译它时,我收到以下错误:

..\src\main.cpp:32:46: error: use of deleted function 'cy::Source::Source(cy::Source&&)' In file included from ..\src\main.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:\mingw\bin../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:\mingw\bin../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&)'

我做错了吗?还是 cppreference 的文档不准确?还是 GCC 4.7.2 有 bug?

最佳答案

事实证明,GCC 的标准库实现还没有实现流类的移动和交换操作。见 here有关 gcc 标准库中 C++11 功能的当前状态的详细信息。

谢谢 Jesse Good用于提供信息和链接。

关于c++ - ifstream的move构造函数是否被隐式删除?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14640366/

31 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com