Created
March 29, 2013 12:32
-
-
Save r2p2/5270534 to your computer and use it in GitHub Desktop.
LANG=C g++ -std=c++0x -O0 main.cpp
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| #include <cstdint> | |
| #include <utility> | |
| #include <string> | |
| #include <vector> | |
| #include <iostream> | |
| class Data | |
| { | |
| public: | |
| explicit Data(uint32_t n) | |
| : _n(n) | |
| , _raw_data(new int[n]) | |
| { | |
| std::cout << "Data(uint32_t) " << _raw_data << std::endl; | |
| for(uint32_t i = 0; i < _n; ++i) | |
| _raw_data[i] = i; | |
| } | |
| Data(const Data& orig) | |
| : _n(orig._n) | |
| , _raw_data(new int[_n]) | |
| { | |
| std::cout << "Data(const Data&) " << _raw_data << std::endl; | |
| for(uint32_t i = 0; i < _n; ++i) | |
| _raw_data[i] = orig._raw_data[i]; | |
| } | |
| Data(Data&& orig) | |
| : _n(orig._n) | |
| , _raw_data(orig._raw_data) | |
| { | |
| std::cout << "Data(Data&&) " << _raw_data << std::endl; | |
| orig._raw_data = 0; | |
| } | |
| ~Data() | |
| { | |
| std::cout << "~Data() " << _raw_data << std::endl; | |
| if(_raw_data) | |
| delete[] _raw_data; | |
| } | |
| Data& operator = (const Data& orig) | |
| { | |
| if(_raw_data) | |
| delete[] _raw_data; | |
| _n = orig._n; | |
| _raw_data = new int[_n]; | |
| std::cout << "operator=(const Data&) " << _raw_data << std::endl; | |
| for(uint32_t i = 0; i < _n; ++i) | |
| _raw_data[i] = orig._raw_data[i]; | |
| return *this; | |
| } | |
| Data& operator = (Data&& orig) | |
| { | |
| if(_raw_data) | |
| delete[] _raw_data; | |
| std::cout << "operator=(const Data&) " << _raw_data << std::endl; | |
| _n = orig._n; | |
| _raw_data = orig._raw_data; | |
| orig._raw_data = 0; | |
| return *this; | |
| } | |
| private: | |
| uint32_t _n; | |
| int* _raw_data; | |
| }; | |
| class DataStorage | |
| { | |
| public: | |
| DataStorage() | |
| : _storage() | |
| {} | |
| explicit DataStorage(size_t reserve_n) | |
| : _storage() | |
| { | |
| _storage.reserve(reserve_n); | |
| } | |
| DataStorage(const DataStorage& orig) | |
| : _storage(orig._storage) | |
| {} | |
| DataStorage(DataStorage&& orig) | |
| : _storage(std::move(orig._storage)) | |
| {} | |
| ~DataStorage() | |
| {} | |
| DataStorage& operator= (const DataStorage& orig) | |
| { | |
| _storage = orig._storage; | |
| return *this; | |
| } | |
| DataStorage& operator= (DataStorage&& orig) | |
| { | |
| _storage = std::move(orig._storage); | |
| return *this; | |
| } | |
| void push_back(Data data) | |
| { | |
| std::cout << "push_back(Data)" << std::endl; | |
| _storage.push_back(std::move(data)); | |
| } | |
| /* if the ouput is correct then these methods are not needed if Data has a move c'tor | |
| void push_back(const Data& data) | |
| { | |
| std::cout << "push_back(const Data&)" << std::endl; | |
| _storage.push_back(data); | |
| } | |
| void push_back(Data&& data) | |
| { | |
| std::cout << "push_back(Data&&)" << std::endl; | |
| _storage.push_back(std::move(data)); | |
| } | |
| */ | |
| private: | |
| std::vector<Data> _storage; | |
| }; | |
| int main() | |
| { | |
| Data da(10); | |
| Data db = da; | |
| Data dc(da); | |
| Data dd(Data(20)); | |
| db = dd; | |
| db = std::move(dd); | |
| DataStorage ds; | |
| ds.push_back(da); | |
| ds.push_back(std::move(dd)); | |
| ds.push_back(Data(29)); | |
| ds.push_back(std::move(Data(29))); | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment