Jump to content
  • Advertisement
Sign in to follow this  
littlekid

Any suggestion to easily add rvalue support?

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

If you intended to correct an error in the post then please contact us.

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 this post


Link to post
Share on other sites
Advertisement
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 this post


Link to post
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 this post


Link to post
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 this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

Participate in the game development conversation and more when you create an account on GameDev.net!

Sign me up!