簡體   English   中英

為什么我不能移動std :: ofstream?

[英]Why can't I move std::ofstream?

看看之前的答案,似乎雖然std::ostream不是可移動的,但std::ofstream應該是。 但是,這段代碼

#include <fstream>

int main()
{
    std::ofstream ofs;
    std::ofstream ofs2{std::move(ofs)};
}

似乎沒有在我試過的任何版本的gcc或clang中編譯(使用--std = c ++ 11或--std = c ++ 14)。 編譯器錯誤有所不同,但這是我得到的gcc 4.9.0

6 : error: use of deleted function 'std::basic_ofstream::basic_ofstream(const std::basic_ofstream&)'

根據標准,這是預期的行為嗎?

請注意,之前問過一個非常類似的問題( std :: ofstream是否可以移動? )但是從那時起標准似乎發生了變化(詳見為什么不能移動std :: ostream? )使這些答案過時。 當然,這些答案都沒有解釋為什么上面的代碼不能編譯。

在嘗試使用ofstream容器時遇到了這個問題,由於上述原因這不起作用。

根據標准

27.9.1.11 basic_ofstream構造函數

或者,它更“可讀”的版本http://en.cppreference.com/w/cpp/io/basic_ofstream/basic_ofstream,std std::basic_ostream<>有一個移動構造函數,所以代碼應該編譯。

clang ++ 3.5用-std=c++11-std=c++1y編譯它。 gcc5也編譯它,所以可能它沒有在libstdc ++中為gcc <5實現

有趣的是,在gcc的stdlibc ++實現中沒有提到缺少移動語義https://gcc.gnu.org/onlinedocs/libstdc++/manual/status.html#status.iso.2014

有關錯誤報告,請參閱https://gcc.gnu.org/bugzilla/show_bug.cgi?id=54316 ,感謝@BoBTFish指出。 確認問題已在gcc5中修復。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM