移动或交换字符串流 [英] Move or swap a stringstream
问题描述
我想移动一个字符串流,在现实世界的应用程序中,我有一些字符串流类数据成员,我想在操作过程中将其重用于不同的字符串.
I want to move a stringstream, in the real world application I have some stringstream class data member, which I want to reuse for different string's during operation.
stringstream没有复制分配或复制构造函数,这很有意义.但是,根据cppreference.com和cplusplus.com,std :: stringstream应该定义了移动分配和交换操作.我都尝试过,但都失败了.
stringstream does not have a copy-assignment or copy constructor, which makes sense. However, according to cppreference.com and cplusplus.com std::stringstream should have a move assignment and swap operation defined. I tried both, and both fail.
#include <string> // std::string
#include <iostream> // std::cout
#include <sstream> // std::stringstream
int main () {
std::stringstream stream("1234");
//stream = std::move(std::stringstream("5678"));
stream.operator=(std::move(std::stringstream("5678")));
//stream.operator=(std::stringstream("5678"));
return 0;
}
prog.cpp:11:56: error: use of deleted function ‘std::basic_stringstream<char>& std::basic_stringstream<char>::operator=(const std::basic_stringstream<char>&)’
stream.operator=(std::move(std::stringstream("5678")));
编译器指出,这三个语句都没有分配拷贝,这是正确的.但是,我看不到为什么不使用移动分配,特别是因为std :: move应该返回右值引用. Stringstream应该具有移动分配,如下所示: http://en.cppreference.com/w /cpp/io/basic_stringstream/operator%3D
The compiler states that there is no copy assignment for all three statements, which is true. However, I fail to see why it is not using the move-assignment, especially since std::move is supposed to return a rvalue reference. Stringstream should have a move assignment, as shown here: http://en.cppreference.com/w/cpp/io/basic_stringstream/operator%3D
PS:我正在使用c ++ 11,因此右值引用是世界"的一部分.
PS: I'm working with c++11, hence rvalue-references are part of the 'world'.
我发现这很奇怪,我从cplusplus.com复制了示例代码,但失败了:
This I found really strange, I copied example code from cplusplus.com and it failed:
// swapping stringstream objects
#include <string> // std::string
#include <iostream> // std::cout
#include <sstream> // std::stringstream
int main () {
std::stringstream foo;
std::stringstream bar;
foo << 100;
bar << 200;
foo.swap(bar);
int val;
foo >> val; std::cout << "foo: " << val << '\n';
bar >> val; std::cout << "bar: " << val << '\n';
return 0;
}
来源: http://ideone.com/NI0xMS cplusplus.com来源: http://www.cplusplus.com/reference/sstream/stringstream/swap/
source: http://ideone.com/NI0xMS cplusplus.com source: http://www.cplusplus.com/reference/sstream/stringstream/swap/
prog.cpp: In function ‘int main()’:
prog.cpp:14:7: error: ‘std::stringstream’ has no member named ‘swap’
foo.swap(bar);
我想念什么?为什么不能移动或交换字符串流?我应该如何交换或移动字符串流?
What am I missing? Why can't I move or swap a stringstream? How should I swap or move a stringstream?
推荐答案
This is a missing feature on GCC : see bug 54316 , it has been fixed (you can thank Jonathan Wakely) for the next versions (gcc 5)
Clang编译以下代码:
Clang with libc++ compiles this code :
int main () {
std::stringstream stream("1234");
std::stringstream stream2 = std::move(std::stringstream("5678"));
return 0;
}
这篇关于移动或交换字符串流的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!