Search code examples
c++c++11rvalue-referencervalue

Does this rvalue signature pattern make sense?


Assume usually I want a copy of the object even if I get a reference. Assume these signatures are within a class scope so that both are seen. What are the pros and cons of doing this as opposed to just having a single push(const RingType& aData). If my object (RingType) only has primitive types or is a primitive type then the pattern probably doesn't buy me anything but does it hurt performance in that case? I feel this pattern is good because if I get a reference I copy it and forward it as an rvalue and if I get an rvalue it just goes to the rvalue signature. Thoughts? My focus here is on performance.

void push(const RingType& aData) { 
  push(RingType(aData));
}

void push(RingType&& aData) {
//process aData
}

Solution

  • It is not very useful:

    • if you do not need a copy, pass by reference
    • if you need a copy, pass by value

    In the latter case, the copy constructor or move constructor will be invoked depending on their availability, for optimal performance.