c++ - C++11 string assignment operator -


look @ code:

#include <iostream> #include <algorithm> #include <fstream> #include <iterator>  using namespace std;  int main() { ifstream text("text.txt");  istreambuf_iterator<char> iis(text); string longest_phrase, _longest;  while (iis != istreambuf_iterator<char>()) {     if ( *iis != '.' ) {         _longest.push_back(*iis);         ++iis;         continue;     }     if ( _longest.size() > longest_phrase.size() )         longest_phrase = move(_longest); //i want move data of _longest longest_phrase. move! not copy!     cout << _longest.empty(); //why _longest not empty??             //_longest.clear();     ++iis; } text.close(); longest_phrase.push_back('.'); cout << "longest phrase " << longest_phrase; return 0; } 

this code searches longest phrase in file. why conversion lvalue rvalue doesnt work?

edit: that's why thought did not work:

class vector { public:     vector(vector<int> &&v): vec( move(v) ) {}     vector<int> vec; };  int main() {     vector<int> ints(50, 44);     vector obj( move(ints) );     cout << ints.empty();     return 0; } 

thank quick , helpful answers!

you should not make concrete assumptions on state of moved-from object of standard library, other fact legal state (unless further post-conditions of move-assignment operator or move constructor specified).

per paragraph 17.6.5.15 of c++11 standard:

objects of types defined in c++ standard library may moved (12.8). move operations may explicitly specified or implicitly generated. unless otherwise specified, such moved-from objects shall placed in valid unspecified state.

moreover, paragraphs 21.4.2/21-23 move-assignment operator of basic_string class template not specify whether moved-from string shall left in state such invoking empty() on returns true.

calling empty() legal in case, since not have pre-conditions on state of string object invoked on; on other hand, cannot make assumptions on return value shall be.


Comments

Popular posts from this blog

c# - Operator '==' incompatible with operand types 'Guid' and 'Guid' using DynamicExpression.ParseLambda<T, bool> -