Public Group

# Any suggestion to easily add rvalue support?

This topic is 2805 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

## Recommended Posts

Hihi, I am trying to add support for rvalue, but adding the extra functions and code have started becoming quite large. Since for every structure, class, I have to add at least 2 more functions (rvalue constructor and rvalue assignment). Furthermore, functions that take in these classes/struct as parameters will also need additional overloads.

e.g
 //formally it is just: void f(const ClassOne& v1, const ClassTwo& v2); //and now it becomes void f(const ClassOne& v1, const ClassTwo& v2); void f(ClassOne&& v1, const ClassTwo& v2); void f(const ClassOne& v1, ClassTwo&& v2); void f(ClassOne&& v1, ClassTwo&& v2); 

and when functions have more parameters the permutations get even worse

Is there any easy way/suggestion around this, which can make it a little easier to maintain and edit??

regards

##### Share on other sites
Why all the const and non-const versions of f? You can pass a non-const object into a function that expects a const reference just fine. So, for each object, check what will be done with it. Will it be modified? Then the function shouldn't promise that it won't - so don't mark that reference as const. Will the object not be modified? Then you can safely mark it as const. Be as strict as possible, but not more strict than that.

Also - and maybe that's because I work more with higher-level languages these days - this looks like a lot of scaffolding code. What exactly will it gain you? Is it worth the effort or is your time better spent on other things? Will you actually use all versions of f? Do you need to distinguish rvalues and lvalues, or will your code work just fine without that distinction?

##### Share on other sites
it is something for a library code. The old code works fine. But using rvalue allow us to take advantage of the move schematic, which is beneficial as the class being passed in can avoid duplication. For example the function may do:

 void Func(const DataKey& key) { table.store(key); //here it does a deep copy of the DataKey } 

but when user does Func(DataKey(100, 200, 300, 400, 500)), we have to do a deep copy of the class even though the DataKey is of r-value type (meaning it would be destroyed immediately)

however if we support r-value, we would be able to just do a shallow copy when the key supplied is of a r-value type. Hence with r-value support the new r-value function would be

 void Func(DataKey&& key) { table.store(std::move(key)); //here it does a shallow copy using DataKey's move operators } 

The motivation for me to add the support for r-value was to avoid the deep-copy of classes which was in the old code.

Given my thoughts, do you think I should just leave the code as it is, or would it be better to support the r-value parameters??

regards

##### Share on other sites
Well, you could template the functions to accept both rvalue and regular references. Ex:
 template <typename T> void Func(T && key) { table.store(std::forward<T>(key)); } 

##### Share on other sites
cool thanks for quick replies.

1. 1
2. 2
3. 3
Rutin
22
4. 4
frob
17
5. 5

• 9
• 33
• 13
• 13
• 10
• ### Forum Statistics

• Total Topics
632582
• Total Posts
3007200

×