Jump to content
  • Advertisement
Sign in to follow this  
Aardvajk

Quick r-value reference question

This topic is 607 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

Hi. Just a quick query that you guys will answer in a snap I'm sure.

So I have a forwarding-constructor to a class. I wrote it like this:

template<typename... Args> BlendValue(Args... args) : v(args...) { }
But when I was peeking at the std::vector implementation that ships with my GCC, at std::vector::emplace_back, I saw they did it like this:

template<typename... Args> void emplace_back(Args&&... args); // double-& before the ellipses
I can't seem to find the implementation of std::vector::emplace_back so I can't then see how they implement the method but I was wondering what the difference was between the two. The way I wrote it originally seems to work okay, but I'm sure my standard library implementors must have their reasons.

Also, should I be doing something like:

template<typename... Args> BlendValue(Args... args) : v(std::forward(args...)) { }
or similar?

Thanks.

Share this post


Link to post
Share on other sites
Advertisement

Also, should I be doing something like: template BlendValue(Args... args) : v(std::forward(args...)) { } or similar?

 

The correct, perfect forwarding solution would be this:

template<typename... Args> BlendValue(Args&&... args) : v(std::forward<Args>(args)...) { }

I still can't give a good understandable explanation as to why you have to use Args&& myself, so I'll just point you to a blog if you are interested:

 

http://eli.thegreenplace.net/2014/perfect-forwarding-and-universal-references-in-c/

 

(or just search for "perfect forwarding" and you should find lots of examples).

Share this post


Link to post
Share on other sites

Also, should I be doing something like: template BlendValue(Args... args) : v(std::forward(args...)) { } or similar?

 
The correct, perfect forwarding solution would be this:
template<typename... Args> BlendValue(Args&&... args) : v(std::forward<Args>(args)...) { }
I still can't give a good understandable explanation as to why you have to use Args&& myself, so I'll just point you to a blog if you are interested:
 
http://eli.thegreenplace.net/2014/perfect-forwarding-and-universal-references-in-c/
 
(or just search for "perfect forwarding" and you should find lots of examples).

 
Simply put, because T&& is special (in contexts where T is deduced), but T alone is not.
 
Consider, for example, this:
template <typename T>
void foo(T);

…

int i = 4;
foo(i);
foo(5);
In both calls, T is deduced to be int – not int&, not int&&, just int. Therefore, the information about whether the function is called with an lvalue or an rvalue is lost there.

If, instead, you change foo to be
template <typename T>
void foo(T&&);
then T will be deduced to either int& (in the foo(i) call) or int&& (in the foo(5) call). Thus you retain the information about whether the function is called with an lvalue or an rvalue.

The article that you linked covers the details of how that works, so I won't repeat them here.

For parameter packs it's the same, except there's multiple of them.

Share this post


Link to post
Share on other sites
Thank you both, excellent stuff. Will have a good look at the article Juliean kindly provided when I have time. And thank you, Oxyd, for the further explanation. Kind of makes sense but need to think it over for a while :)

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.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!