Thread: How to take advantage of the Move Semantics (with code)?

  1. #1
    Registered User
    Join Date
    Feb 2017
    Posts
    3

    How to take advantage of the Move Semantics (with code)?

    PHP Code:
    I read your interesting article:http://www.cprogramming.com/c++11/rvalue-references-and-move-semantics-in-c++11.htmland tried to follow the contained suggestionsBut after many trials I still do not understand how to properly take advantage of the move semantics in order to not copy the result of the operation and just use the pointer, or std::move, to "exchange" the data pointed to. This will be very usefull to speed-up more complicated functions like f(g(),h(i(l,m),n(),p(q()))
    Executing the below code the result is
    Code:
    t3={2,4,6} and t1={1,2,3}
    PHP Code:
    while the goal is to have
    Code:
    t3={2,4,6} and t1={}
    Code:
      typedef std::vector<double> Tensor1DType;
    
      class Tensor1D {
      private:
        //std::shared_ptr<Tensor1DType> data = std::make_shared<Tensor1DType>();
        Tensor1DType * data = new Tensor1DType;
      public:
        Tensor1D() {
        };
        Tensor1D(const Tensor1D& other) {
          for(int i=0;i<other.data->size();i++) {
            data->push_back(other.data->at(i));
          }
        }
    
        Tensor1D(Tensor1D&& other) : data(std::move(other.data)) {
          other.data = nullptr;
        }
    
        ~Tensor1D() {
          delete data;
        };
        int size() {
          return data->size();
        };
    
        void insert(double value) {
          data->push_back(value);
        }
    
        void insert(const std::initializer_list<double>&  valuesList) {
          for(auto value : valuesList) {
            data->push_back(value);
          }
        }
    
        double operator() (int i) {
          if(i>data->size()) {
            std::cout << "index must be within vector dimension" << std::endl;
            exit(1);
          }
          return data->at(i);
        }
    
        Tensor1D& operator=(Tensor1D&& other)  {
          if (this == &other){
            return *this;
          }
          data = other.data;
          other.data = nullptr;
          return *this;
        }
    
        void printTensor(Tensor1DType info) {
          for(int i=0;i<info.size();i++) {
            std::cout << info.at(i) << "," << std::endl;
          }
        }
        void printTensor() {
          for(int i=0;i<data->size();i++) {
            std::cout << data->at(i) << "," << std::endl;
          }
        }
      };
    
    
    } // end of namespace MTensor
    
    MTensor::Tensor1D scalarProduct1D(MTensor::Tensor1D t1, double scalar) {
      MTensor::Tensor1D tensor;
      for(int i=0;i<t1.size();++i) {
        tensor.insert(t1(i) * scalar);
      }
      //return std::move(tensor);
      return tensor;
    }
    
    
    int main() {
      MTensor::Tensor1D t1;
      t1.insert({1,2,3});
      std::cout << "t1:" << std::endl;
      t1.printTensor();
      MTensor::Tensor1D t3(scalarProduct1D(t1,2));
      std::cout << "t3:" << std::endl;
      t3.printTensor();
      std::cout << "t1:" << std::endl;
      t1.printTensor();
      return 0;
    }

  2. #2
    Hurry Slowly vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,788
    since data is just a pointer(I do not really know why - instead of having vector itself) - you do not need std::move - just point it to the new location

    Code:
        Tensor1D(Tensor1D&& other) : data(other.data) {
          other.data = nullptr;
        }
    PS. In your assignment operator - you need also to delete old data - in case you assign to already filled object
    All problems in computer science can be solved by another level of indirection,
    except for the problem of too many layers of indirection.
    – David J. Wheeler

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. How to take advantage of the move semantics?
    By Marco_I in forum C++ Programming
    Replies: 0
    Last Post: 02-27-2017, 08:40 AM
  2. Trouble with move semantics...
    By MutantJohn in forum C++ Programming
    Replies: 4
    Last Post: 11-16-2015, 08:30 PM
  3. Replies: 2
    Last Post: 11-22-2012, 12:01 AM
  4. move semantics / rvalue references
    By m37h0d in forum C++ Programming
    Replies: 12
    Last Post: 12-08-2011, 01:58 PM
  5. Rvalue references and move semantics in C++11
    By webmaster in forum C++ Programming
    Replies: 14
    Last Post: 11-17-2011, 04:01 AM

Tags for this Thread