Thread: Trouble with move semantics...

  1. #1
    Registered User MutantJohn's Avatar
    Join Date
    Feb 2013
    Posts
    2,665

    Trouble with move semantics...

    Hey guys, I have some code and I'm having trouble understanding why my default constructor call isn't being treated as an rvalue reference. The STL accepts it properly so it must be something with my implementation, I'm just having trouble seeing where...

    Code:
    template<class T>
    class Element {
    private:
      char buffer[sizeof(T)];
      bool alive_;
    
    
    public:
      Element(void) {
        std::cout << "calling Element default constructor" << std::endl;
        alive_ = false;
        memset(&buffer, 0, sizeof(T));
      }
    
    
      Element(const T &t) {
        std::cout << "calling Element(T) copy constructor" << std::endl;
        new(buffer) T(t);
        alive_ = true;
      }
    
    
      Element(T &&t) {
        std::cout << "calling Element(T) move constructor" << std::endl;
        new(buffer) T(std::move(t));
        alive_ = true;
      }
    
    
      ~Element(void) {
        std::cout << "calling Element destructor" << std::endl;
        if (alive_) {
          T *tmp = (T* ) buffer;
          tmp->~T();
          alive_ = false;
        }
      }
    
    
      T& operator*(void) {
        T *tmp = reinterpret_cast<T*>(buffer);
        return *tmp;
      }
    };
    with test code:
    Code:
    struct MyStructure {
      int *data;
      int size;
    
    
      MyStructure(void) {
        std::cout << "calling MyStructure default constructor" << std::endl;
        size = 256;
        data = new int[size];
      }
    
    
      MyStructure(const MyStructure &other) {
        std::cout << "calling MyStructure copy constructor" << std::endl;
        size = other.size;
        data = new int[size];
      }
    
    
      MyStructure(MyStructure &&other) : data(other.data), size(other.size) {
        std::cout << "calling MyStructure move constructor" << std::endl;
        other.data = nullptr;
        other.size = 0;
      }
    
    
      ~MyStructure(void) {
        size = 0;
        delete[] data;
      }
    
    
      friend std::ostream& operator<<(std::ostream &os, MyStructure &a);
    };
    
    
    std::ostream& operator<<(std::ostream &os, MyStructure &a) {
      os << a.size;
      return os;
    }
    
    
    MyStructure test(void) {
      MyStructure tmp;
      return tmp;
    }
    
    
    int main(void) {
      MyStructure a;
      Element<MyStructure> el2(a);
      std::cout << *el2 << std::endl;
    
    
      MyStructure &&rvalue = MyStructure();
      Element<MyStructure> el3(std::move(rvalue));
      std::cout << *el3 << std::endl;
    
    
      Element<MyStructure> el4(std::move(MyStructure()));
      std::cout << *el4 << std::endl;
    
      // this is the problem one!
      Element<MyStructure> el5(MyStructure());
      std::cout << *el5 << std::endl;
    
    
      std::vector<MyStructure> x;
      x.push_back(MyStructure());
      std::cout << x[0].size << std::endl;
    
    
      return 0;
    }
    I've been getting this compilation error which leads me to believe that el5 is actually receiving a function pointer instead of the return from the default constructor call. But it works for std::vector!

    Code:
    warning: the address of ‘Element<MyStructure> el5(MyStructure (*)())’ will always evaluate as ‘true’ [-Waddress]
       std::cout << *el5 << std::endl;

  2. #2
    Master Apprentice phantomotap's Avatar
    Join Date
    Jan 2008
    Posts
    5,108
    O_o

    What does a `int s(int());` segment do in code?

    Soma
    “Salem Was Wrong!” -- Pedant Necromancer
    “Four isn't random!” -- Gibbering Mouther

  3. #3
    Registered User MutantJohn's Avatar
    Join Date
    Feb 2013
    Posts
    2,665
    Huh, I get the same thing. You can't move construct a default construction, can you? Does this mean I need the move assignment operator?

  4. #4
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Quote Originally Posted by MutantJohn View Post
    You can't move construct a default construction, can you?
    Everything is movable as long as it has move constructor/move assignment operator.

    Does this mean I need the move assignment operator?
    Nope.
    You need to disambiguate the syntax. Try:

    Code:
    Element<MyStructure> el5(MyStructure{});
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  5. #5
    Registered User MutantJohn's Avatar
    Join Date
    Feb 2013
    Posts
    2,665
    Holy poop, that works! Or at least, it works for the int example so I trust it.

    Thank you so much, Elysia.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Trouble: Program to move strings from 1 file to another.
    By ScotchandTweed in forum C Programming
    Replies: 2
    Last Post: 03-07-2013, 04:02 PM
  2. move semantics / rvalue references
    By m37h0d in forum C++ Programming
    Replies: 12
    Last Post: 12-08-2011, 01:58 PM
  3. Rvalue references and move semantics in C++11
    By webmaster in forum C++ Programming
    Replies: 14
    Last Post: 11-17-2011, 04:01 AM
  4. semantics and syntaxes
    By kk01 in forum C++ Programming
    Replies: 3
    Last Post: 01-04-2008, 07:02 AM
  5. semantics
    By anjana in forum Tech Board
    Replies: 4
    Last Post: 10-10-2006, 10:35 AM