C++ 11 move semantics vs C++ 98 -


i've studied c++ 11 move semantics , have such question.

for example:

if have

vector<t> vt; // assume t have pointers on data in separate memory vt.push_back(...); 

assume vt lacks unused capacity. in c++98 allocated more memory , copy (calls constructor copy) every t object data points on.

for example:

t1 -> t1 data => copied t_cop1 -> (t1_cop1 data) t2 -> t2 data => t_cop2 -> (t2_cop2 data)

c++ 11 move semantics allow move t objects (just copy pointers, not copy data in separate memory calling move constructors).

so question why cannot same in c++98, why cannot implement push_back copy memory(which contains pointers t1, t2 t_cop1 , t2_cop2 created same pointers) , after free (t1 , t2 using free(void*) example)?

upd: ok, try explain question on more simple example:

for instance if have class contains pointer datastructure data. implement copy constructor copies pointer "data"

class { public:     a() {};     a(const a& a) {         data = a.data;     } private:     struct data{};     data* data; };  // , want implement "move semantics" calling copy constructor (that copies pointer)  a* = new a(); a* b(a);  // , somehow free "a" object (but not call destructor) free(a); // or = null 

as understand that's move semantics (but leave in consistent state). think had ability implement move in old c++. is't it?

also can same in previous example vector

"move semantics" provides way compiler pass references things constructors can differentiated lvalue references (the kind you're used to, found in c++98) , rvalue references (new c++11, using && notation, these refer objects temporary , intermediate, , not last past current statement).

so can write copy constructor ("make new object copy of one") , different move constructor ("make new object copy of one, , oh way source object temporary destroyed next, if want optimize in way doesn't leave source object intact, go ahead").

so example, in container class, can copy container class copy constructor, , new copy contain full duplicate of contents (which means things element-by-element copy construction of contents happen). move constructor can skip work , swap internals source container, taking on contents of source container , leaving source container empty. (later source container gets destroyed, should trivial since it's empty.) can done in constant time instead of o(n) or worse.

you can't equivalent as constructor (and temporary object) in c++98 because knows how make copy. there way achieve similar effect via swap() function (and method in classes), had written explicitly. (basically, move contents of container a newly-constructed empty container b, call swap(a, b);, destroy a empty.)


one example of case can't right thing in c++98 initializing vector result of function returns vector:

vector<int> fibonacci(int n) {      vector<int> result;     if(n <= 0) return result;     result.push_back(0);     if(n <= 1) return result;     result.push_back(1);     for(int = 1; < n; ++i)         result.push_back(result[i] + result[i - 1]);     return result;  }  vector<int> datavect = fibonacci(10); 

c++98 , c++11 both elide away function return copy (creating result in spot return value stored) c++98 can call copy constructor datavect because there no move semantics. c++11 can call move constructor instead, can move storage temporary return value datavect. there's not way in c++98 without getting operator new , operator delete involved , returning pointers instead of objects, beating on (non-deterministic time) free store no gain.


Comments

Popular posts from this blog

google api - Incomplete response from Gmail API threads.list -

Installing Android SQLite Asset Helper -

Qt Creator - Searching files with Locator including folder -