4

I tried to read about it and also found a couple of similar questions on the internet, but they were either not directly related to my issue or a little confusing. My questions are as follows:

  1. It appears that move semantics are only about the (memory of) objects that are contained in a class - not for the class itself, or the primitive data types in it. If that is correct, is it of any use at all for creating a Move constructor for a class that contains 4 to 5 different integer, long and float member variables, for example? This is a considerable chunk of memory for let's say some high performance application. If all that memory is still going to be allocated for temporary objects in a function and then again when you assign that to a suitable lvalue, it doesn't solve the purpose completely and passing the object as reference in the argument might still be the way to go. Am I correct in this assumption? Is there a way to make it work so that return by value works for my object and the memory for primitive data members is also not allocated twice? (I know I can create a separate class/struct containing all the primitive types to create one object and then including its pointer in my class as a member, but that seems to be a rather clumsy solution.)
  2. Can defining a move constructor ever be useful for a single primitive member class?

This is not a case of premature optimization, I'm just trying to understand it as it's a little different from what I was expecting.

Edit: @Paul, Nicol here's a simple example of what I'm trying to do. Can I be sure that RVO will take care of creating the object only once(let's say in both MSVC11 and GCC 4.8)?

class MyObject
{
public: //public members for the purpose of this example
    long long mNum1;
    int mNum2;
public:
    MyObject GetModifiedObject()
    {
        MyObject retVal;
        retVal.mNum1 = SomeGlobalMethod1(mNum1);
        retVal.mNum2 = SomeGlobalMethod2(mNum2);
        return retVal;
    }
};

int main(int, char*)
{
    MyObject obj;
    obj.mNum1 = -2232344;
    obj.mNum2 = 25;
    MyObject obj2 = obj.GetModifiedObject();
}

Thanks!

Piyush Soni
  • 1,356
  • 3
  • 17
  • 40
  • 1
    move semantics are useful only for resources managed by a class. pointers handles etc. Things that can be quickly swapped between instances. If you have a member that resides within the actual memory block of a class/struct, you can only copy it. Can you give an actual concrete example, as return value optimitation might give you what you want. – Paul Rooney Aug 16 '16 at 03:19
  • "*Is there a way to make it work so that return by value works for my object and the memory for primitive data members is also not allocated twice?*" In what code? It's not clear what "my object" you're talking about here. – Nicol Bolas Aug 16 '16 at 03:31
  • @PaulRooney, NicolBolas. I have edited the question. I was asking if there is a way for Move semantics to help me here by not even creating a second object and copying the primitives one by one. – Piyush Soni Aug 16 '16 at 03:43
  • @NicolBolas: Exact duplicate of "what are move semantics"? I'm not sure. – Piyush Soni Aug 16 '16 at 03:45
  • Move semantic will help because you won't have to know the inners of your object. But there is nothing usefull to do in your code. Also current compilers are good enough to remove unecessary temporaries by themselves if there are only local ressources and some are not needed anymore. – kriss Aug 25 '16 at 09:51

0 Answers0