0

Passing a this pointer assigned to another one works fine, but passing just it itself directly doesn't as below:

table_row* table_row::deserialize_row(std::string src_serialized_row) {
        std::stringstream ss(src_serialized_row);
        boost::archive::text_iarchive ia(ss);
        table_row * dest = this;
        ia >> dest; // this is fine, compiles.
        return dest;
    }
table_row* table_row::deserialize_row(std::string src_serialized_row) {
        std::stringstream ss(src_serialized_row);
        boost::archive::text_iarchive ia(ss);
        ia >> this; //error, >> operator does not match [error]
        return this;
    }

[error] I don't really understand this. I'm passing the same pointer in both code examples, right? Why would it be an error?

Alan Kałuża
  • 515
  • 5
  • 19
  • 2
    It's hard telling where the problem could be without looking at more code. Please post a [mcve]. – R Sahu Aug 14 '19 at 16:46
  • 1
    Remember that just because something compiles it doesn't necessarily mean it's logically correct. Deserializing like this to a pointer doesn't feel right. usually `>>` operates on a reference to a value. – user4581301 Aug 14 '19 at 17:02
  • 3
    `is >> this`, is illegal for the same reason that `this = that;` is illegal. You can't assign to the `this` pointer. – john Aug 14 '19 at 17:13
  • thanks @john I realised exactly what i'm doing with >> operator and it makes sense. – Alan Kałuża Aug 22 '19 at 16:29

1 Answers1

5

The only difference is that this is a prvalue, and assigning it to dest would make it an lvalue.

I would assume that the operator looks something like this:

template<class T>
boost::archive::text_iarchive& operator>>(
    boost::archive::text_iarchive& ia,
    T& archive_to
);

And an rvalue like this can't bind to the non-const lvalue reference, as it's trying to set the pointer to the deserialised value (probably not what you want).

Artyer
  • 31,034
  • 3
  • 47
  • 75
  • 3
    Some other reading to help understand the above: [What are rvalues, lvalues, xvalues, glvalues, and prvalues?](https://stackoverflow.com/questions/3601602/what-are-rvalues-lvalues-xvalues-glvalues-and-prvalues) – user4581301 Aug 14 '19 at 16:54